I prefer a simpler approach:
Am I missing anything the lookbehind provides?s/$c$a/$c$b/g; or s/($c)$a/$1$b/g;
</Editor's Note>
All right... here's a benchmark of the two methods (lookbehind and just straight substitution). It looks like lookbehind is slightly faster, but not by a huge lot. Here's the code:
And here are the benchmark results:use Benchmark; use vars qw/$a $b $c/; ($a, $b, $c) = qw/bar baz foo/; timethese(shift || 1, { lookbehind => sub { local $_ = "foobarquux"; s/(?<=$c)$a/$b/g +}, chromatic => sub { local $_ = "foobarquux"; s/($c)$a/$1$b/g +}, });
Benchmark: timing 500000 iterations of chromatic, lookbehind... chromatic: 15 wallclock secs (14.46 usr + 0.00 sys = 14.46 CPU) lookbehind: 13 wallclock secs (11.93 usr + 0.00 sys = 11.93 CPU)
In reply to Re: How do i find $a and replace it with $b when it's preceded by $c?
by btrott
in thread How do i find $a and replace it with $b when it's preceded by $c?
by whahoo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |