|Perl Monk, Perl Meditation|
Re: Look-Arounds in Regexes are Hardby papidave (Monk)
|on Jul 27, 2009 at 20:57 UTC||Need Help??|
++moritz for caring about beginners.
I don't consider myself to be a newby any more, but even so, look-wherever expressions make me a bit crosseyed, and it got worse when I realized that the original question implied a fixed start and end to the pattern. When that happens, a line like
should match the first code block, but not the second.
If we want to use lookahead to match <code> blocks that don't contain foo, we might use something like
but you'd be wise to insert a ton of comments to clarify things*. OTOH, I find it far more readable to use something like the following:
This gives us the desired multiple-block behaviors, while providing us with the non-foo data (in $1) for each instance, so we can do something more complicated with it if desired. In addition, the nested loop was surprisingly about 10% faster than the look-ahead code when I compared it over 100,000 iterations using Perl v5.10.0.
*Unless you're in the UK; in that case, use a tonne of comments instead. :)
Note: lost a slash in code cut/paste business - repaired.