XML technologies / Regular expressions / How regex engines work / regex-directed and text-directed engines

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

The two engine types apply regexes in fundamentally different ways. I call the gasoline-powered NFA regex-directed, and the electric DFA text-directed.

 

NFA: regex-directed

Consider an NFA applying ˹to(nite|knight|night)˼ to ›...tonight...‹. It tests each regex component, starting with ˹t˼, against current input. Success advances to the next component; matching them all gives overall success.

In the ˹to(nite|knight|night)˼ example, the initial ˹t˼ fails until the input has t. Then test the next component, ˹o˼. After success, the next element is ˹(nite|knight|night)˼, meaning “˹nite˼ or ˹knight˼ or ˹night˼”. The engine tries each branch in order. We immediately see that tonight requires the third branch. The regex-directed engine, despite its neural ancestry (see Origins of regular expressions), must try the alternatives to know.

Each branch follows the same procedure: “Try ˹n˼, then ˹i˼, then ˹t˼, and finally ˹e˼.” Failure moves to the next branch. Regex structure controls the engine, hence regex-directed.

NFA advantage: the regex controls matching

Subexpressions operate nearly independently, except backreferences and concatenation. Their structural layout—alternatives, groups, quantifiers—determines the search.

This gives the regex author control over the search (Practical regex techniques and The art of writing regular expressions discuss this). Details will soon become clear.

 

DFA: text-directed

Text-directed engines instead process input characters while tracking every viable candidate. In the tonight example, after reading t they know a candidate begins:

in the string

in the regex

after ...t▵onight...

possible matches: ˹t▵o(nite|knight|night)˼

Each character updates the candidate set. Soon only two remain, since knight is impossible:

in the string

in the regex

after ...toni▵ght...

possible matches: ˹to(ni▵te|knight|ni▵ght)˼

After g , only one remains. After h and t, the engine knows a match exists and finishes successfully.

Every input character controls the engine, hence text-directed. A partial match can represent many candidates, eliminated progressively. A complete shorter match may already exist: with ˹to(...)?˼, the optional group is still explored greedily, but the whole regex already matches ›to‹.

If the next character eliminates all candidates, return a saved complete match. Without one, this starting point fails and the engine restarts later.

 

An initial NFA/DFA comparison

You may suspect DFAs are faster: NFAs repeatedly compare branches against the same text.

That suspicion is correct. An NFA tests the same subject character with many components, often even the same component repeatedly. A successful subexpression may be applied again as part of the overall strategy. Its local success or failure cannot be judged finally until the regex’s end. (If I knew how to work “The game is not over until 90 minutes are up” into this paragraph, I would.) By contrast, a DFA is deterministic : every character is examined at most once. It may belong to a candidate later discarded, but saved states mean it never needs reexamination.

The formal names are Nondeterministic finite automaton (translator's note: German literature also uses “finite automaton”, hence NEA/DEA) (NFA), and Deterministic finite automaton (DFA). I will stick to the abbreviations. (Note: theory explains these names; deterministic is important, but practical consequences matter more here than theory, as this chapter will show.)

 

Implications for users

NFA search details matter because the regex controls them. Authors influence matching through formulation. Our tonight example might waste less work if written differently:

  • ˹to(ni(ght|te)|knight)˼

  • ˹tonite|toknight|tonight˼

  • ˹to(k?night|nite)˼

These match identical text but take different paths. Soon we can assess the tradeoffs.

With DFAs it is the opposite: because all candidates are tracked simultaneously, equivalent formulations make no difference. There may be hundreds of paths, all stored and followed at once, almost magically. Even regexes as different as ˹abc˼ and ˹[aa-a](b|b{1}|b)c˼ are indistinguishable to a pure DFA.

Three things describe DFAs (translator's note: “DFA engine” is redundant; automaton denotes the mathematical model, engine its implementation in a tool like egrep):

  • DFAs are fast.

  • DFA matching is consistent.

  • Describing their matching is boring.

We will revisit all three.

By contrast, there is much to say about regex-directed NFA matching, which offers more scope for artistic expression. A cleverly built regex runs efficiently; a poor formulation can put the engine in serious trouble. Combustion engines are not the only ones that can stall. To understand this, we must reach the NFA’s central mechanism: backtracking.

  

 

 

 

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