In this particular case there's possibly a faster method though:
s/^(?!(?>.*?toto.*?foo))(.*?)foo/${1}bar/s;
If toto or foo can be found in the near beginning, Abigail's pattern is a winner. This is, I guess, due to the overhead of the
(?>) assertion. But the farther away toto or foo is the worse it gets for the step-by-step method (
/(?:(?!foo).)*/), and the
(?>) pattern gets faster. (This isn't just a theoretical win. Even though the overhead due to
(?>), that pattern might very well win in a real-life situation.)
But if I shall look at the big picture for a while though; in this case I find it hard to believe that any pattern can beat the plain and simple
s/foo/bar/ unless /toto.*?foo/s;
Even silly constructions like
s/foo(?(?{index($`, 'toto') != -1})(?!))/bar/;
is faster than the more generic solutions.
Cheers,
-Anomo
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.