Perl/Regex question (hopefully simple)

G. Matthew Rice matt-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org
Tue Jun 29 17:01:14 UTC 2004


Madison Kelly <linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org> writes:
> $help_body =~ s/\[url=(.*)\](.*)\[\/url\]/<a href="\1"
> target="_new">\2<\/a>/gi;
> 
> Which seems very much like what you both recommended. Do you know if one
> method is better or worse than the other or are they just two ways to do the
> same thing?

Read the perlre manpage.  Scan for the section 'Warning on \1 vs $1'.  Plus
you get the $1, $2, ... after a m// operation is over.

Aww, what the heck.  Here it is:

       Warning on \1 vs $1

       Some people get too used to writing things like:

           $pattern =~ s/(\W)/\\\1/g;

       This is grandfathered for the RHS of a substitute to avoid
       shocking the sed addicts, but it's a dirty habit to get
       into.  That's because in PerlThink, the righthand side of
       a "s///" is a double-quoted string.  "\1" in the usual
       double-quoted string means a control-A.  The customary
       Unix meaning of "\1" is kludged in for "s///".  However,
       if you get into the habit of doing that, you get yourself
       into trouble if you then add an "/e" modifier.

           s/(\d+)/ \1 + 1 /eg;        # causes warning under -w

       Or if you try to do

           s/(\d+)/\1000/;

       You can't disambiguate that by saying "\{1}000", whereas
       you can fix it with "${1}000".  The operation of interpo-
       lation should not be confused with the operation of match-
       ing a backreference.  Certainly they mean two different
       things on the left side of the "s///".

HTH,
-- 
g. matthew rice <matt-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org>         starnix, thornhill, ontario, ca
phone: 905-771-0017 x242                                  gpg id: EF9AAD20
http://www.starnix.com              professional linux services & products
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list