Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

by Incognito (Pilgrim)
on Nov 21, 2001 at 01:24 UTC ( #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? - Answer 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 } +, });

Log In?

What's my password?
Create A New User
[marioroy]: Lady_Aleena Angels on earth are real people. That's awesome.
[Lady_Aleena]: marioroy, what does MCE stand for in this instance? There are 3 pages of abbreviations to go through.
[Lady_Aleena]: ^ or Discipulus...
[marioroy]: Many-core Engine
[choroba]: MCE
[karlgoethebier]: is about to reach nirvana tonight...
[Lady_Aleena]: It could have meant a "Miserably Cute Event" or "Man Crush Everyday". 8)
[Corion]: choroba: Re the one-shot thing, I also thought of bit vectors and/or indexes into one common array from the hashes, but that makes maintenance of all these indices a chorse
[Corion]: *core
[Corion]: ** chore

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2017-05-29 08:28 GMT
Find Nodes?
    Voting Booth?