On Monday 17 July 2006 18:20, Timothy Johnson wrote:
> \1 is the same thing as $1 inside of a regex, but it is generally
> recommended that you don't use it.

I thought it's ok to use it in a match or on the *left* side (but not on the 
right side) of a substitution

>> then I'm stumped, what's the 1 for?
>>
>> /([a-zA-z]{3})\s*\1/

That appears to me to be the match, *not* the substitution

the \1 refers to what gets captured via the parenthesis and means "another of 
the same as what was captured via parenthesis"

I'm still learning but I think I got that correct.  But please do correct me 
if I've mistaken in anything that I've said.

>
> From 'perldoc perlre':
>
> "   Warning on \1 vs $1
>     Some people get too used to writing things like:
>
>         $pattern =~ s/(\W)/\\\1/g;

note:  it's on the *right* side of substitution

>
>     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 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

again on the *right* of substitution

>
>     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 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///"."

AFAIK \1 is ok to use on left or in match

> -----Original Message-----
> From: Ryan Dillinger [mailto:[EMAIL PROTECTED]
> Sent: Monday, July 17, 2006 6:10 PM
> To: beginners@perl.org
> Subject: pattern matching
>
> Hello All,
> I was studying some pattern matching. And I ran into this piece of code.
> Now I believe I understand it up until the the last part \1.
> Can someone explain it for me please?
> Match lowercase a through z, uppercase A through lc z
> no more than three times, with white space zero or one times
> then I'm stumped, what's the 1 for?
>
> /([a-zA-z]{3})\s*\1/
>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to