Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to regex (\G) vs. (^|\G)

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.

Replies are listed 'Best First'.
Re: Re: regex (\G) vs. (^\G)
by Rhandom (Curate) on May 15, 2001 at 20:31 UTC
    Good point about the hang. On our system where we discovered the problem it did actually hang.

    Thanks for the info -- that answer seems to make a lot of sense, and looks like a rather big caveat for regex writers.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://80601]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found