Search code examples
regexregexbuddy

regexbuddy sometime do not backtrack


When i use the regex IID:\s*\d*0 to match ID: 12344y the regexbuddy give me the result

https://raw.github.com/litsand/litsand.github.com/master/_posts/pic/4.png

it backtrack \d* to find the match ,but don't backtrack the \s*

when i change the regex to ID:\s*\d*q ,it don't backtrack anymore.and give me the fail message.

https://raw.github.com/litsand/litsand.github.com/master/_posts/pic/5.png

I know even if it backtrack ,finally the regex would give me a fail message. But how the regexbuddy knew it would fail and don't backtrack?

I read the Mastering Regular Expressions and don't find any answer. thanks for your help.

Sorry for the pictures,I don't have the right to upload image.


Solution

  • RegexBuddy's regex engine internally optimizes your regular expressions into ID:\s*+\d*0 and ID: \s*+\d+q using possessive quantifiers. It can do this because \s and \d are mutually exclusive, as are \d and q. Mastering Regular Expressions calls this "automatic possessification".

    In RegexBuddy 3, the regex debugger also uses this optimization. That's why you didn't see the backtracking steps in the debugger. In RegexBuddy 4, the regex debugger has all optimizations disabled. In RegexBuddy 4 the debugger will show all the backtracking your regular expression does in a regex engine that doesn't have "automatic possessification".