XML technologies / Regular expressions / How regex engines work / More on greediness / Backtracking in lookaround
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
It may not be obvious that lookaround, introduced in Extended introductory examples (see Splitting large numbers into groups of three: lookaround), is closely related to atomic groups and possessive quantifiers. Lookaround has four forms: positive and negative lookahead and lookbehind. They test whether their subexpression matches from the current position, for lookahead, or up to it, for lookbehind.
How does lookaround work in the NFA world of saved states and backtracking? Its subexpression runs in a small world of its own, saving states and backtracking as needed. What happens once the whole subexpression has matched? For positive lookaround, the construct succeeds; for negative lookaround, it fails. Only the existence of a match matters, and we have assumed one was found. The entire little world, including all its saved states, is then discarded.
If the inner expression fails, it can access only states created within that lookaround. Backtracking to its beginning means the subexpression cannot match. Positive lookaround then fails; negative lookaround succeeds. No states remain, otherwise evaluation would not be complete. Its little world has effectively disappeared on its own.
Thus lookaround never leaves saved states behind. Any still present when a successful positive lookahead finishes are discarded on exit. Where have we seen states discarded before? In atomic groups and possessive quantifiers.
Emulating atomic groups with positive lookahead
For dialects supporting atomic groups, this is mostly academic. For others: If positive lookahead is supported and if capturing parentheses are allowed within it, as in most dialects except Tcl, atomic groups or possessive quantifiers can be emulated. ˹(?>Regex)˼ can be replaced with ˹(?=(Regex))\1˼. For example, compare ˹^(?>\w+):˼ with ˹^(?=(\w+))\1:˼.
In the lookahead version, ˹\w+˼ tries to consume as much as possible, a whole word. Since it is inside lookaround, its saved states are discarded at the closing parenthesis, as with an atomic group. Unlike an atomic group, its matched characters do not enter the overall match, but the capturing parentheses save them. That is the key: the subsequent backreference ˹\1˼ was just defined by the lookahead, guaranteeing that ˹\1˼ matches. This extra step simply moves the regex engine’s current position beyond the already matched word.
This is slightly less efficient than real atomic groups, since ˹\1˼ must match the same text again. Discarding useless saved states nevertheless makes the whole regex faster than plain ˹\w+:˼ when the string has no colon.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln