A
Arvin Portlock
I know how to store a regular expression using qr() but
I can't figure out a way to store the substitution part
of a substitution. For example, this works:
my $example = 'I match this string';
my $match = 'this string';
my $repl = 'that string';
my $regexp = qr($match);
$example =~ s/$regexp/$repl/;
and I get 'I match that string';
But I want to do something like this:
my $example = 'I saw Bob and Alice';
my $match = 'I saw (.*) and (.*)';
my $repl = 'I saw $2 and $1';
my $regexp = qr($match);
$example =~ s/$regexp/$repl/;
print $example;
However this gives me 'I saw $2 and $1' and what I want
is 'I saw Alice and Bob'.
Is there any way to do what I want? I'd like to know the
exact mechanics rather than using some module or another.
Also, is there a way to store the entire substitution, a la
my $subst = qr(s/$regexp/$repl/)?
I can't figure out a way to store the substitution part
of a substitution. For example, this works:
my $example = 'I match this string';
my $match = 'this string';
my $repl = 'that string';
my $regexp = qr($match);
$example =~ s/$regexp/$repl/;
and I get 'I match that string';
But I want to do something like this:
my $example = 'I saw Bob and Alice';
my $match = 'I saw (.*) and (.*)';
my $repl = 'I saw $2 and $1';
my $regexp = qr($match);
$example =~ s/$regexp/$repl/;
print $example;
However this gives me 'I saw $2 and $1' and what I want
is 'I saw Alice and Bob'.
Is there any way to do what I want? I'd like to know the
exact mechanics rather than using some module or another.
Also, is there a way to store the entire substitution, a la
my $subst = qr(s/$regexp/$repl/)?