Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Answer: How do i find $a and replace it with $b when it's preceded by $c?

( #126648=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > How do i find $a and replace it with $b when it's preceded by $c? contributed by Incognito

To summarize what's been done by chromatic, btrott and nuance, with the chromatic version using no backreferences named chromatic2, we have:
Benchmark: timing 500000 iterations of chromatic, chromatic2, lookbehi +nd... chromatic: 18 wallclock secs (18.83 usr + 0.01 sys = 18.84 CPU) @ 265 +43.50/s chromatic2: 13 wallclock secs (13.55 usr + 0.00 sys = 13.55 CPU) @ 369 +00.37/s lookbehind: 14 wallclock secs (14.95 usr + 0.00 sys = 14.95 CPU) @ 334 +42.58/s
for the test:
use Benchmark; use vars qw/$a $b $c/; ($a, $b, $c) = qw/bar baz foo/; timethese(shift || 500000, { lookbehind => sub { local $_ = "foobarquux"; s/(?<=$c)$a/$b/g +}, chromatic => sub { local $_ = "foobarquux"; s/($c)$a/$1$b/g +}, chromatic2 => sub { local $_ = "foobarquux"; s/$c$a/$c$b/g } +, });

Comment on Answer: How do i find $a and replace it with $b when it's preceded by $c?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-12-25 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (161 votes), past polls