XML technologies / Regular expressions / How regex engines work / backtracking / Saved states
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
In NFA terminology, the breadcrumbs are called saved states. A state describes a situation from which a different alternative can be tried. It stores both the position in the string and the position in the regex where an untried option begins. Since this underlies NFA matching, I will explain it more fully with other examples. Readers satisfied with the explanation so far may skip ahead.
A match without backtracking
Consider a simple example: ˹ab?c˼ is applied to abc . After the initial ˹a˼ matches, the current state is:
in the text: › | in the regex: ˹ |
Next comes ˹b?˼ in the regex, and the engine must decide whether to try ˹b˼. Since the question mark is greedy, it tries this option, first saving the state
in the text: › | in the regex: ˹ |
in the initially empty list of saved states. This lets the engine later return to the point immediately after the ˹b?˼, or immediately before the b in the string—in other words, to the current state. This skips the question mark.
Having left these breadcrumbs, the engine tests the following ˹b˼ in the regex; the new current state is:
in the text: › | in the regex: ˹ |
The final ˹c˼ matches too, completing the entire regex match. The single saved state is no longer needed and is discarded.
A match after backtracking
If the subject string is ›ac‹, everything is the same until the attempt with ˹b˼ . This time, however, no b is found in the string. The path that began by trying ˹...?˼ has proved a dead end. Since saved states remain, this local failure is not necessarily a global one. The engine performs backtracking, making the last saved state current. Here, that state is:
in the text: › | in the regex: ˹ |
This was saved as an untried path before testing ˹b˼. This time, ˹c˼ matches c, completing the entire regex match.
A failure
Now apply the same expression to the string abX. Before trying ˹b˼, the question mark causes the state
in the text: › | in the regex: ˹ |
to be saved. The ˹b˼ matches, but this path later reaches a dead end because ˹c˼ does not match the X. This local failure triggers backtracking to the saved state. The engine now compares ˹c˼ with the b whose match backtracking has effectively revoked. This test also fails, of course. If other saved states remained, further backtracking would follow. Here, there is only one, so the entire attempt at the current position fails.
Are we done? No: the transmission advances to the next character and tries the entire regex again on the remaining string. In a sense, this is pseudo-backtracking. Matching resumes at
in the text: › | in the regex: ˹ |
. The entire regex is applied again, but all attempts fail as before. After the next two attempts, starting at ab▵X and abX▵, also fail, the overall result is failure.
A lazy match
Return to the original example, now with a lazy quantifier: ˹ab??c˼ is applied to abc.
in the text: › | in the regex: ˹ |
Next comes ˹b??˼, and the engine must decide whether to try or skip the option ˹b˼. Because the quantifier is lazy (??), the first attempt does not try this option. To preserve the other possibility for later,
in the text: › | in the regex: ˹ |
is added to the still-empty list of saved states. The engine can thus return to the position immediately before the b in the text and try ˹b˼. We know it will match, but the regex engine does not know yet, nor that it will ever make this attempt. After saving the state, the engine proceeds:
in the text: › | in the regex: ˹ |
The ˹c˼ does not match ›b‹, so the engine must backtrack to its one saved state.
in the text: › | in the regex: ˹ |
This time the option is tried. It matches, and the following ˹c˼ matches the ›c‹. We obtain the same overall match as with the greedy quantifier, but by a different route.
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