XML technologies / Regular expressions / How regex engines work / NFA, DFA, and POSIX
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The leftmost-longest match
Recall DFA behavior: when the transmission starts a DFA at a particular string position, it finds the longest possible match there, and that is all. Because this match is both longest and starts furthest left, we call it the leftmost-longest match.
Truly the longest match
Longest-match behavior concerns more than alternation. Consider an NFA applying the expression ˹motor(rad)?(radfahren)?˼ to the string motorradfahren . The NFA first finds a local match for ˹motor˼, then the greedy ˹(rad)?˼ matches, leaving only ˹(radfahren)?˼ to compare with fahren . This fails, but that is harmless because the expression is optional, and the regex has now ended. A traditional NFA therefore finds motorradfahren and ignores saved states. A POSIX NFA behaves differently, as we will see.
A DFA, however, finds the longer motorradfahren. An NFA would find it too if the first ˹(rad)?˼ were somehow given back, allowing ˹(radfahren)?˼ to match. Traditional NFAs do not do this; DFAs do because it is the longest match for the current attempt. A DFA can do so because it keeps track of all possible matches at all times and can choose the longest.
I chose this example because it is easy to discuss, but the point matters in practice too. A common task is to include continuation lines in a match. Configuration files often allow one logical line to span multiple physical lines using a backslash before a newline, as in:
SRC=array.c builtin.c eval.c field.c gawkmisc.c io.c main.c \
missing.c msg.c node.c re.c version.cTo match a simple “var = wert” line, one might use ˹^\w+=.*˼, but it ignores continuation lines. For this example, assume dot does not match newline. To support continuation lines, we could append ˹(\\\n.*)*˼ to the regex, giving ˹^\w+=.*(\\\n.*)*˼. This should match each additional line following a backslash-newline pair. It looks reasonable, but fails with a traditional NFA. When the first ˹.*˼ reaches the first line ending, it has already passed the backslash, and none of the new regex elements forces Backtracking (see Rule 2: ordinary quantifiers are greedy). A DFA does find the multiline match, simply because it is the longest possible match.
If lazy quantifiers are supported, one might try them here, for example: ˹^\w+=.*?(\\\n.*?)*˼. This tests for backslash-newline before dot has matched anything, potentially allowing the subexpression ˹\\˼ to match. Unfortunately, it still fails. A lazy quantifier matches optional text only when forced. Here, however, every element following ˹=˼ is optional; nothing forces the lazy star to do anything. The entire lazy regex matches only ›SRC=‹, which is certainly not a solution.
Other techniques can solve this problem. We return to the example in Practical regex techniques (see A few short examples).
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