Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex: changing longer patterns to shorter ones

Reply
Thread Tools

Regex: changing longer patterns to shorter ones

 
 
Steventangle
Guest
Posts: n/a
 
      03-23-2005
ITEM 0

How does one go about changing a longer pattern to a shorter one? For
instance, I try

$string =~ tr/longer/short/;

but it doesn't change "This is a longer pattern." to "This is a short
pattern." Rather, it does a one-for-one replacement yielding "This is a
shortr pattrro."

ITEM 1

How do you use variables in regexes? For instance, if I wanted to replace
the variable $var with $string, what's the tr or s syntax for it?

Thanks for the help,
Steve


 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-23-2005
Steventangle wrote:
> How does one go about changing a longer pattern to a shorter one? For
> instance, I try
>
> $string =~ tr/longer/short/;
>
> but it doesn't change "This is a longer pattern." to "This is a short
> pattern."


Not surprising, since you are using the tr/// operator, which doesn't
include any pattern or regular expression.

> Rather, it does a one-for-one replacement yielding "This is a
> shortr pattrro."


Yep, that's what the tr/// operator does.

Try the s/// operator instead.

perldoc perlop

> How do you use variables in regexes? For instance, if I wanted to replace
> the variable $var with $string, what's the tr or s syntax for it?


s/$var/$string/

perldoc perlop

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      03-23-2005
Steventangle <(E-Mail Removed)> wrote:

> How do you use variables in regexes?



The same way that you use them in double quoted strings.


> For instance, if I wanted to replace
> the variable $var with $string, what's the tr or s syntax for it?



Show us what you have tried and we will help you fix it.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      03-23-2005
Steventangle wrote:

> ITEM 0
>
> How does one go about changing a longer pattern to a shorter one? For
> instance, I try
>
> $string =~ tr/longer/short/;
>
> but it doesn't change "This is a longer pattern." to "This is a short
> pattern." Rather, it does a one-for-one replacement yielding "This is a
> shortr pattrro."


That's correct. That's what tr/// does. What you want is s///
>
> ITEM 1
>
> How do you use variables in regexes? For instance, if I wanted to replace
> the variable $var with $string, what's the tr or s syntax for it?


You type $var right into the regex. It does interpolate, you know.
>
> Thanks for the help,
> Steve


--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
Reply With Quote
 
StevenTangle
Guest
Posts: n/a
 
      03-23-2005
Yes, Gunnar, you were right on the money. Converting a longer string to a
shorter string became very easy after I used s/longer/short as
tr/longer/short does a one-to-one replacement of the letters. Also,
s/$var/$string works fines.

Thanks!
Steve

"Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Steventangle wrote:
>> How does one go about changing a longer pattern to a shorter one? For
>> instance, I try
>>
>> $string =~ tr/longer/short/;
>>
>> but it doesn't change "This is a longer pattern." to "This is a short
>> pattern."

>
> Not surprising, since you are using the tr/// operator, which doesn't
> include any pattern or regular expression.
>
>> Rather, it does a one-for-one replacement yielding "This is a shortr
>> pattrro."

>
> Yep, that's what the tr/// operator does.
>
> Try the s/// operator instead.
>
> perldoc perlop
>
>> How do you use variables in regexes? For instance, if I wanted to
>> replace the variable $var with $string, what's the tr or s syntax for it?

>
> s/$var/$string/
>
> perldoc perlop
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Longer and shorter USB extension leads john bently Computer Support 3 02-17-2010 06:52 AM
Can I turn 1 long column into 3 shorter ones? trvlDev ASP .Net 3 04-06-2007 08:41 PM
Longer array out of two shorter arrays Mik0b0 C Programming 15 01-13-2007 08:38 PM
conversion of longer int to shorter ones junky_fellow@yahoo.co.in C Programming 2 01-12-2007 10:44 AM
curiosity-- long links in coding to shorter ones? Richard HTML 2 07-10-2003 05:04 PM



Advertisments