[tpm] New Regex question - partially solved

Rob Janes janes.rob at gmail.com
Sun Oct 28 09:35:08 PDT 2012


Regarding the accusation that I did not read the docs.  Here's an excerpt
from man perlre (the highlights are mine):

   Warning on \1 Instead of $1
       Some people get too used to writing things like:

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

       This is *grandfathered* (for \1 to \9) 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 an "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


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

So you see, \1 in the replace is not deprecated, it is grandfathered.
Everybody, feel free to keep using it, although it's use may cause
reviewers of your code to point and stare, perhaps gesticulate.

stylistically, I'd say if you're using \1 on the match side, don't switch
to $1 on the replace side.  It's confusing.

As for being greatly mistaken about \1 and $1, I think not.  \1 and $1 are
slightly different beasts on the match side of an s/// regex.  They both
work, and are not deprecated.  On the replace side, \1 and $1 are
equivalent, there is no deprecation of \1.  I've made specific statements
about how \1 and $1 behave on the match side so as to better understand
their differentiation.  I've posted tested examples as well, and snippets
of the documentation.  So, Uri, what exactly is it that I'm greatly
mistaken about?

I am not sure what to make of the bottom posting remark.  Perhaps it
doesn't belong in this thread?

fyi, The code snippets I posted were tested with perl 5.14.


On Sun, Oct 28, 2012 at 12:02 PM, Uri Guttman <uri at stemsystems.com> wrote:

> On 10/28/2012 06:45 AM, Rob Janes wrote:
>> ok, if you don't believe me when I say it doesn't work, try it yourself
>> then.
>>  i haven't tried it as i said i didn't test it.
>  [robj at rj-ul80vt ~]$ perl -e '$x = "one \t\t two"; print "$x\n"; $x =~
>> s/(\s)$1+/\1/; print "$x\n";'
>> one          two
>> one two
>> [robj at rj-ul80vt ~]$ perl -e '$x = "one \t\t two"; print "$x\n"; $x =~
>> s/(\s)\1+/\1/; print "$x\n";'
>> one          two
>> one      two
>> notice that the usage of $1 resulted in a mixed string of whitespace being
>> substituted.  only the tabs should have been substituted, not everything.
>> therefore, the $1 in the matching regex resolved to \s not space.
>>  please read the docs. you are greatly mistaken about \1 and $1. you keep
> using \1 in the replacement where it doesn't belong. why it seems to work
> is another story. when i get to playing with the code i will figure it out.
> too busy today.
> and learn to bottom post too.
> uri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20121028/c7b4bd75/attachment.html>

More information about the toronto-pm mailing list