Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: regex backtracking question

by aeqr (Novice)
on Apr 24, 2014 at 01:11 UTC ( [id://1083493]=note: print w/replies, xml ) Need Help??


in reply to Re: regex backtracking question
in thread regex backtracking question

Thanks for your answer, the thing I don't understand is how it makes the difference when the engine fails. In both cases, the match occurred. In the first case,how is it possible that it doesn't try to match ABC again? After all, it matched once, failed and thus gets another chance to match exactly 3 characters at the position it failed.

Is it different because \w{2,}? has a sort of counter that remembers how long was the last attempt? And so it extends the match when it tries to run again because matching a longer chain is seen as a "new match that didn't occur here before"?

Replies are listed 'Best First'.
Re^3: regex backtracking question
by AnomalousMonk (Archbishop) on Apr 24, 2014 at 01:34 UTC

    The regex engine (RE) always tries to make the first overall match it can at the leftmost point in the string at which a match is possible. In the case of  \w{3} the leftmost point is the first character in the string, 'A' in this case. After taking exactly three characters, the RE tries to satisfy  (?!) (discounting the  (?{ code }) bit, which doesn't figure in to the match) to complete an overall match, which is impossible. The only choice the RE has is to advance the leftmost match point, in this case to 'B', and then try, try again.

    In the case of  \w{2,} (either lazy or greedy), the RE has other options: just take one more/give up one  \w character (if possible) and try again for an overall match, which fails, ... The "sort of counter that remembers" is the Non-deterministic Automaton backtracking mechanism of the NFA RE.

    Gotta go...

      Ok, I got it, thanks for the explanation.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found