laziness, impatience, and hubris | |
PerlMonks |
Re: regex (\G) vs. (^\G)by chipmunk (Parson) |
on May 15, 2001 at 20:24 UTC ( [id://80601]=note: print w/replies, xml ) | Need Help?? |
Yep, this is a bug alright. It's hit a lot of programmers, unfortunately.
This bug was created by an optimization in the regular expression engine. As I understand it, here's the basic idea: if the regex contains \G, then the engine doesn't need to worry so much about the value of pos() when it's setting up for the match, because that'll be taken care of when the \G is matched. Unfortunately, the optimization doesn't account for the \G being optional; when that's the case, then the value of pos() isn't taken into account and the regex matches the same part of the string over and over again. The best way to avoid the bug is to make sure when you use \G that any successful match will include \G. BTW, your first example doesn't actually hang; it goes into an infinite loop, printing [a] endlessly.
In Section
Seekers of Perl Wisdom
|
|