Skip to main content
added 601 characters in body
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

Don't use strings to build regular expressions, use qr//. There's one important difference: when two regular expressions created by qr// are concatenated, each of them is first wrapped in a (?:...). When two strings are concatenated, you just get a string containing the first string followed by the second one. But $base contains a pipe symbol whose range will be restricted by the enclosing (?:...). Cf.

printmy $s = "A',"";
my $r = 'A|\{';

print $s =~ $_ ? 1 : 0
    for qr/A|\{$r,/,      # 1
        qr/(A|\{?:$r),/;  # 0

Also, using a variable containing a variable name doesn't interpolate the second variable in a regex, only one level of interpolation happens:

my $r = 'A';
my $s = '$r';
print "A" =~ /$s/;  # Doesn't match.

Moreover, \1 in a double quoted string (and replacement in a substitution behaves like a double quoted string) means the chr(1). Perl uses $1 to refer to the first matching group. But again, using a variable containing $1 wouldn't expand it, as only one level of interpolation happens.

my $s = 'ABC';
my $r = '$1';
say $s =~ s/(A)BC/$r/r;  # $1

You can use the /ee modifier to force the second interpolation, but then the replacement part must be Perl code syntactically, e.g.

my $s = 'ABC';
my $r = '"(" . $1 . ")"';
say $s =~ s/A(B)C/$r/ree;  # (B)

Don't use strings to build regular expressions, use qr//. There's one important difference: when two regular expressions created by qr// are concatenated, each of them is first wrapped in a (?:...). When two strings are concatenated, you just get a string containing the first string followed by the second one. But $base contains a pipe symbol whose range will be restricted by the enclosing (?:...). Cf.

print "A'," =~ $_ ? 1 : 0
    for qr/A|\{,/,
        qr/(A|\{),/;

Also, using a variable containing a variable name doesn't interpolate the second variable in a regex, only one level of interpolation happens:

my $r = 'A';
my $s = '$r';
print "A" =~ /$s/;  # Doesn't match.

Don't use strings to build regular expressions, use qr//. There's one important difference: when two regular expressions created by qr// are concatenated, each of them is first wrapped in a (?:...). When two strings are concatenated, you just get a string containing the first string followed by the second one. But $base contains a pipe symbol whose range will be restricted by the enclosing (?:...). Cf.

my $s = "A',";
my $r = 'A|\{';

print $s =~ $_ ? 1 : 0
    for qr/$r,/,      # 1
        qr/(?:$r),/;  # 0

Also, using a variable containing a variable name doesn't interpolate the second variable in a regex, only one level of interpolation happens:

my $r = 'A';
my $s = '$r';
print "A" =~ /$s/;  # Doesn't match.

Moreover, \1 in a double quoted string (and replacement in a substitution behaves like a double quoted string) means the chr(1). Perl uses $1 to refer to the first matching group. But again, using a variable containing $1 wouldn't expand it, as only one level of interpolation happens.

my $s = 'ABC';
my $r = '$1';
say $s =~ s/(A)BC/$r/r;  # $1

You can use the /ee modifier to force the second interpolation, but then the replacement part must be Perl code syntactically, e.g.

my $s = 'ABC';
my $r = '"(" . $1 . ")"';
say $s =~ s/A(B)C/$r/ree;  # (B)
Source Link
choroba
  • 49.4k
  • 7
  • 92
  • 118

Don't use strings to build regular expressions, use qr//. There's one important difference: when two regular expressions created by qr// are concatenated, each of them is first wrapped in a (?:...). When two strings are concatenated, you just get a string containing the first string followed by the second one. But $base contains a pipe symbol whose range will be restricted by the enclosing (?:...). Cf.

print "A'," =~ $_ ? 1 : 0
    for qr/A|\{,/,
        qr/(A|\{),/;

Also, using a variable containing a variable name doesn't interpolate the second variable in a regex, only one level of interpolation happens:

my $r = 'A';
my $s = '$r';
print "A" =~ /$s/;  # Doesn't match.