I am performing a string substitution in Perl, but I have both the pattern and the replacement strings stored as scalar variables outside the regular expression operators. The problem is that I want the replacement string to be able to use backreferences.
I hope the code below will illustrate the matter more clearly.
my $pattern = 'I have a pet (\w+).';
my $replacement = 'My pet $1 is a good boy.';
my $original_string = 'I have a pet dog.';
# Not Working
my $new_string = $original_string =~ s/$pattern/$replacement/r;
# Working
#my $new_string = $original_string =~ s/$pattern/My pet $1 is a good boy./r;
# Expected: "My pet dog is a good boy."
# Actual: "My pet $1 is a good boy."
print "$new_string\n";
s/$pattern/My pet $1 is a good boy./
is short for
s/$pattern/ "My pet $1 is a good boy." /e
The replacement expression ( "My pet $1 is a good boy."
) is a string literal that interpolates $1
.
This means that
s/$pattern/$replacement/
is short for
s/$pattern/ "$replacement" /e
The replacement expression ( "$replacement"
) is a string literal that interpolates $replacement
(not $1
).
While it may be hindering you, it's a good thing that perl
isn't in the habit of executing the contents of variables as Perl code. :)
You can use gsub_copy
from String::Substitution to solve your problem.
use String::Subtitution qw( gsub_copy );
my $pattern = 'I have a pet (\w+)\.';
my $replacement = 'My pet $1 is a good boy.';
my $original_string = 'I have a pet dog.';
my $new_string = gsub_copy($original_string, $pattern, $replacement);
That $1
in the replacement string is just successive chars $
and 1
, and to make it into a variable for the first capture you'd have to go through bad hoops. †
How about an alternative
my string = q(a pet dog);
my $pattern = qr/a pet (\w+)/;
my $new = $string =~ s/$pattern/ repl($1) /er;
sub repl {
my ($capture) = @_;
return "$capture is a good boy";
}
where the sub is really just
sub repl { "$_[0] is a good boy" }
It's a little more but then it's more capable and flexible.
† Or, as it turns out per ikegami's answer, use String::Substitution which wraps up all the involved 'niceties' into a single call
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.