s/$match/$replace/ fails when $replace has backreferences

A

anonyusenet

I need to store my match and replace strings in variables.

This fails when my match string uses back references to groupings.

Here for example I try to replace "foo" with "foobar" using a back
reference:

#!/usr/bin/perl
$content = "this is my foo";
$match = "(foo)";
$replace = "\1bar";
#$replace = "\\1bar"; # this doesn't work either
#$replace = "$1bar"; # neither does this
$content =~ s/$match/$replace/;
print "$content\n"


Any ideas how to get this to work? I need the match and replace strings
in variables because I am reading them from file.

Thanks!
 
C

ced

I need to store my match and replace strings in variables.

This fails when my match string uses back references to groupings.

Here for example I try to replace "foo" with "foobar" using a back
reference:

#!/usr/bin/perl
$content = "this is my foo";
$match = "(foo)";
$replace = "\1bar";
#$replace = "\\1bar"; # this doesn't work either
#$replace = "$1bar"; # neither does this
$content =~ s/$match/$replace/;
print "$content\n"


Any ideas how to get this to work? I need the match and replace strings
in variables because I am reading them from file.


One possibility:

# perldoc -q expand and consider a hash instead of a double eval
# Also see perlre: \<digit> vs. $<digit> discussion

$replace = '"$1bar"'; # or $replace = '$1 . "bar"'
$replace =~ s/$match/$replace/ee;
die $@ if $@;

htm,
 
T

Tad McClellan

I need to store my match and replace strings in variables.

This fails when my match string uses back references to groupings.

Here for example I try to replace "foo" with "foobar" using a back
reference:

#!/usr/bin/perl


Please have the courtesy of posting programs that work with both:

use strict;
use warnings;

$content = "this is my foo";
$match = "(foo)";
$replace = "\1bar";

$replace has a 4-character string in it now since the double
quotes ate the backslash.

#$replace = "\\1bar"; # this doesn't work either


You should use the backslash-digit form only *within* a
regular expression.

The replacement part of an s/// is not a regular expression,
so you should use the dollar-digit form of backref.

#$replace = "$1bar"; # neither does this


The double quotes interpolate the value of $1 _now_ rather than
further down in the code after $1 has been set.

$content =~ s/$match/$replace/;
print "$content\n"


Any ideas how to get this to work?


------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $content = "this is my foo";
my $match = "(foo)";
my $replace = '"$1bar"'; # double-quotish for 2nd "e"val
#my $replace = '$1 . "bar"'; # concatenation for 2nd "e"val
$content =~ s/$match/$replace/ee;
print "$content\n"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top