in reply to Re^2: regex backtracking question
in thread regex backtracking question
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...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: regex backtracking question
by aeqr (Novice) on Apr 24, 2014 at 01:47 UTC |