in reply to Re^6: Regex infinite loop?
in thread Regex infinite loop?

First, I thought that if I used the "?" I got non-greedy searching. Shouldn't that be helping things out?
You are right about the first part. The secondary ? quantifier makes the match non-greedy. But greedy/non-greedy only makes a (possible) difference if there is a match. It will not change the fact whether something will or will not match. And while there might be a difference in performance when there is a match (it could go either way, but Friedl suggests that using ? is slower in most cases), there will usually not be much of a performance difference if there's no match. Perl will try all possible lengths before giving up, and it hardly matters when starting from longest match working towards shortest or starting with shortest working up to longest.
Within the capturing parentheses you have [^"]++. What does this mean exactly?
It means, match as many characters that aren't double quotes, and once you've found that many, do not try with less characters if the regexp engine backtracks to this point. The not "giving back" characters is the meaning of the second +, and was introduced in 5.10. The reason I used it here is that in:
something[^"]++"
once the regexp engine has matched 'something', a string of non-double quotes and then a double quote, and the rest of the pattern fails, and hence, the engine backtracks to the matching of the string of non-double quote characters, it's pointless to try it with one character less in the string of non-double quote characters: after all, the next character has to be a double quote.

Replies are listed 'Best First'.
Re^8: Regex infinite loop?
by Ninth Prince (Acolyte) on Oct 17, 2008 at 16:09 UTC

    Thank you. This helps my understanding tremendously.

Re^8: Regex infinite loop?
by Ninth Prince (Acolyte) on Oct 17, 2008 at 17:03 UTC

    Wow, I made the change that you suggested and it is super fast. The jets and colts now complete in about 1-2 seconds rather than 1-2 hours. COOL! Thank you!