XML technologies / Regular expressions / How regex engines work / Backtracking

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

NFA matching works essentially as follows: the engine examines each subexpression in order. When facing two possibilities, it chooses one but remembers the other.

Every quantifier, which can match in several ways, and every alternation offers such choices: which alternative now, which later?

If the chosen direction succeeds and the rest of the regex matches, the match is found and searching ends. Otherwise, the engine knows where to return and try other possibilities. It can thus explore every permutation, or enough to find a match. This return to earlier branch points is called backtracking.

Backtracking like Hansel and Gretel

Backtracking is like leaving breadcrumbs at every fork. If the chosen direction leads to a dead end, retrace your steps until you find crumbs. If the next route is also blocked, go back farther, until the correct path is found or no untried routes remain.

There are several situations where the regex engine must choose between possibilities; alternation is one. With an element such as ˹...x?...˼, the engine must decide whether to test ˹x˼ now or postpone it. With ˹...x+...˼ there is no choice: plus demands at least one matching character. After the first ˹x˼ matches, however, no further requirement remains. The engine must decide whether to seek a second ˹x˼, then a third, and so forth. At every decision, it leaves a symbolic fairy-tale breadcrumb to remember the other possibilities.

A crumbly example

Consider again our regex ˹to(nite|knight|night)˼ applied to the string ›hot●tonic●tonight!‹, a contrived but useful example. First, the engine tests whether the initial component ˹t˼ matches at the start of the string. It does not match h, so the whole regex fails at that position. The transmission advances to the next character, also unsuccessful, then the third. This time, ˹t˼ matches, but the following ˹o˼ does not, so the attempt fails.

A later attempt starting at ...▵tonic... is more interesting. After to matches, the engine faces three alternatives. It chooses one and remembers the branch point, leaving crumbs in case it fails. Suppose it chooses ˹nite˼. This subexpression breaks down into “˹n˼ + ˹i˼ + ˹t˼...”, reaching ...toni▵c.... Unlike earlier failures, alternatives remain: the engine need not restart and advance one character. It tries another alternative, say ˹knight˼, which immediately fails. The third remains, ˹night˼, but it too eventually fails. That exhausts the options; the entire attempt starting at ...▵tonic... has failed, and the transmission must advance.

When the engine reaches ...▵tonight!, the next attempt becomes interesting again. This time, the ˹night˼ alternative matches through the end of the regex. The whole expression has matched and the engine can return success.

  

  

 

 

 

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

back to top