I was curious about whether the anchoring really reduced the work, so I ran a benchmark of the various suggested methods. I found some of the results surprising -- like the fact that xeger was not a lot slower than the anchored match. And anchoring the 2-step solution slowed it down.
@strings = ( 'There was a bright man from Nantucket'
,'Abba babbles about bubble blowers'
,'Enough with the excess bs already'
,'None at all in this one');
my @c;
use Benchmark 'cmpthese';
%methods = (
'anch' => sub { s/b(?=[^b]*$)/x/ for @c=@strings},
'xeger' => sub { $_ = reverse, s/b/x/, $_ = reverse for @c=@strings
+},
'plain' => sub { s/(.*)b/$1x/ for @c=@strings},
'2part' => sub { /.*(?=b)/g and s/\Gb/x/ for @c=@strings },
'2part-A' => sub { /b(?=[^b]*$)/g and s/\Gb/x/ for @c=@strings },
'substr' => sub { substr($_, rindex($_, 'b'), 1) = 'x' for @c=@strin
+gs},
'neglook' => sub { s/b(?!.*b)/x/ for @c=@strings }
);
cmpthese(-3, \%methods );
Results:
Rate plain 2part-A 2part neglook xeger anch substr
plain 3208/s -- -31% -35% -44% -47% -51% -64%
2part-A 4630/s 44% -- -6% -20% -23% -29% -48%
2part 4948/s 54% 7% -- -14% -18% -24% -45%
neglook 5767/s 80% 25% 17% -- -4% -12% -36%
xeger 6029/s 88% 30% 22% 5% -- -8% -33%
anch 6536/s 104% 41% 32% 13% 8% -- -27%
substr 8985/s 180% 94% 82% 56% 49% 37% --
Caution: Contents may have been coded under pressure.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.