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.
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)/
In reply to Re: Difference between $1 and \1.
by 7stud
in thread Difference between $1 and \1.
by rbala
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |