Nonbacktracking Lookahead and Lookbehind
Positive lookahead and lookbehind do not backtrack. That is, their contents are treated in the same way as the contents of a nonbacktracking (?> )
group.
Because lookahead and lookbehind are always zero-width, backtracking behavior is visible only when capturing groups appear within positive lookahead and lookbehind. For example, the expression (?=(a*))\1a
will never find a match because group 1, which is defined within the lookahead, consumes as many "a" characters as there are, then \1a
requires one more. Because the lookahead expression is not backtracked, the matching engine does not retry group 1 with fewer "a" characters.
For more on grouping, lookahead, and lookbehind constructs, see Grouping Constructs