Regex: changing longer patterns to shorter ones

Discussion in 'Perl Misc' started by Steventangle, Mar 23, 2005.

  1. Steventangle

    Steventangle Guest

    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
    Steventangle, Mar 23, 2005
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Mar 23, 2005
    #2
    1. Advertising

  3. Steventangle <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 23, 2005
    #3
  4. 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?"
    Chris Mattern, Mar 23, 2005
    #4
  5. Steventangle

    StevenTangle Guest

    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" <> wrote in message
    news:...
    > 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
    StevenTangle, Mar 23, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard
    Replies:
    2
    Views:
    375
    Jacqui or (maybe) Pete
    Jul 10, 2003
  2. crichmon
    Replies:
    4
    Views:
    480
    Mabden
    Jul 7, 2004
  3. Replies:
    2
    Views:
    286
    CBFalconer
    Jan 12, 2007
  4. Mik0b0

    Longer array out of two shorter arrays

    Mik0b0, Jan 13, 2007, in forum: C Programming
    Replies:
    15
    Views:
    411
    Keith Thompson
    Jan 13, 2007
  5. trvlDev
    Replies:
    3
    Views:
    263
    trvlDev
    Apr 6, 2007
Loading...

Share This Page