Think of it this way: \w{2,}? matches the minimum possible of two or more 'somethings', in this case \w characters. On its first try it matches two characters (and prints them). The match is then intentionally 'failed', so \w{2,}? gets another chance to match beginning at the point at which it previously attempted the match (not at that point + 1), this time matching three characters. And so on through four, five, ... Only when the regex engine runs out of \w thingies to match is it forced to move the initial match point forward and try some more, starting again with two characters.
Contrast with the behavior of "ABCDEF" =~ /(\w{2,})(?{print "$1\n"})(?!)/; (no ? quantifier modifier: greedy matching).
In the original "ABCDEF" =~ /([A-Z]{3})(?{print "$1\n"})(?!)/; case, [A-Z]{3} can only match exactly three characters, so the regex engine must advance the trial match point upon failure; no backtracking (or forward-tracking, if that's even a correct term, in the case of lazy matching) is possible: exactly three characters and some other stuff were required to match at a given point; the match failed; move on — nothing to see here, folks.
Update: Sorry: many small additions, wording changes, etc., which should not have altered essential meaning.
In reply to Re: regex backtracking question
by AnomalousMonk
in thread regex backtracking question
by aeqr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |