rbala has asked for the wisdom of the Perl Monks concerning the following question:

I used the following regex to find repeating characters in a string.
$str = aa; $str =~ /(a)$1/; print "$&";
This does not match 'aa'. It matched only 'a'. When i replaced with \1 , i got the output .
$str = aa; $str =~ /(a)\1/; print "$&";
Can anyone explain this behaviour ?

Replies are listed 'Best First'.
Re: Difference between $1 and \1.
by 7stud (Deacon) on Dec 27, 2012 at 10:50 UTC
    $str = aa;

    Unless you have a variable named aa somewhere, that line will give you an error.

    The rule is: $1 is used inside strings and \1 is used inside regexes.

    Examine the inputs for the substitution operator:

    s/regex/string/

    Here are some examples:

    my $line = 'abbc'; $line =~ s/(.)\1/XX/g; say $line; --output:-- aXXc
    my $line = 'abbc'; $line =~ s/(.)/X$1X/g; say $line; --output:-- XaXXbXXbXXcX
    my $line = 'abbc'; $line =~ s/(.)/$1$1/g; say $line; --output:-- aabbbbcc

    I used the following regex to find repeating characters in a string.

    $str = aa; $str =~ /(a)$1/; print "$&";
    This does not match 'aa'. It matched only 'a'.

    A regex acts like a double quoted string with respect to variable interpolation (and with respect to string escape sequences). $1 has not been assigned a value by a previous match, so $1 is equal to undef. And when undef is used like a string, perl converts it to a blank string. Interpolating undef into a double quoteish context, like your regex, means you are interpolating a blank string, so your regex is equivalent to: /(a)/

      $str = aa;

      Unless you have a variable named aa somewhere, that line will give you an error.

      It is only an error when strict subs are in effect.
      { no strict 'subs'; my $str = aa; say $str; }
      Though having strict on is almost always a good thing.
Re: Difference between $1 and \1.
by LanX (Saint) on Dec 27, 2012 at 11:38 UTC
    > Can anyone explain this behaviour ?

    Google finds plenty of them by entering your title:

    Perl Difference between $1 and \1

    Now what are your problems left?

    Within the matching part of regexes $1 can't be used for your purpose!

    It's still bound to the match of the last regex:

    DB<107> $_="a"; /(\w)/; $_='aaa'; /($1+)/; print $1 aaa

    And regexes are older than Perl, \1 was the usual way to address first match, but variables with $-sigils belong to the syntax of scripting languages like Perl.

    Cheers Rolf

Re: Difference between $1 and \1.
by 2teez (Vicar) on Dec 27, 2012 at 12:04 UTC
    Checking the perl documentation, one could get glorious insight.

    So, doing
    perldoc -v $1 Update I: OR check perlvar
    from the CLI gives:

    $<*digits*> ($1, $2, ...):
    Contains the subpattern from the corresponding set of capturing parentheses from the last successful pattern match, not counting patterns matched in nested blocks that have been exited already. These variables are read-only and dynamically-scoped.

    While \1 :
    is Backreference to a specific capture group or buffer.
    Please also check perlre.

    It is also a good thing to note this Warning on \1 Instead of $1
    UPDATE I: I was only checking this on Win OS. Thanks to LanX comments below.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      > perldoc -v $1

      before people start complaining, this doesn't work with 5.10!¹

             -v   Describes search for the item in detail (verbosely).

      And when using Linux, better care about hiding $1 from bash-expansion.

      perldoc -v '$1'

      Cheers Rolf

      ¹) dunno when it was introduced...

      UPDATE:

      CHANGES

      Up to 3.14_05, the switch -v was used to produce verbose messages of perldoc operation, which is now enabled by -D.

      ehm 3?

        Thanks all! From your replies I understand that $1 , $2 type of variables will not contain any value or contain only, old match values of first or second paranthesis from any previous regex matching , unless the pattern matching is complete and if that matching is successful. Opposed to this is \1 which stores first paranthesis value even before the entire pattern is matched , so that it has a defined value in the matching pattern itself and hence provides expected results. Hope my understanding is correct. Correct me if any concerns for you...!!!
Re: Difference between $1 and \1.
by Anonymous Monk on Dec 27, 2012 at 10:19 UTC