XML technologies / Regular expressions / How regex engines work / Start the engine! / Types of regex engines
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
There are two fundamental types of regex engines: DFA, the electric motor in our analogy, and NFA, the combustion engine. We will soon explain what NFA and DFA mean; for now, they are just names like Hans and Fritz, or electric and gasoline.
Both have existed for a long time, but, as with motors, the NFA type is used much more often. NFA engines power the .NET languages, PHP, Ruby, Perl, Python, GNU Emacs, ed, sed, vi, most versions of grep , and a few egrep and awk programs. DFAs occur in the vast majority of versions of egrep and awk, as well as lex and flex. Some systems have a hybrid engine, using one type or the other depending on the task, or even applying different types to different parts of a regex to maximize versatility and speed. The following table lists engine types for several programs. If yours is not listed, use the method under Identifying the regex engine type.
Table: Programs and their regex engine types.
Engine type | Programs |
|---|---|
DFA | awk (most versions), egrep (most versions), flex, lex, MySQL, Procmail |
Traditional NFA | GNU Emacs, Java, grep (most versions), less, more, the .NET languages, the PCRE library, Perl, PHP (all three regex packages), Python, Ruby, sed (most versions), vi |
POSIX NFA | mawk, Mortice Kern Systems utilities, GNU Emacs (when requested) |
Hybrid NFA/DFA | GNU awk, GNU grep/egrep, Tcl |
As Features and dialects showed, twenty years of development have produced many unnecessary variations in both DFAs and NFAs—an unfortunate situation. The POSIX standard was created to remedy it by specifying exactly which metacharacters an engine must recognize and, above all, which results it must return. Apart from details, DFAs, our electric motors, already largely met these requirements. Traditional NFAs, however, produce results quite different from those required by the standard, so a POSIX-compliant NFA needs substantial changes. We can therefore broadly divide regex engines into three groups:
DFA (POSIX or otherwise—similar in either case)
Traditional NFA (the usual case: Perl, .NET, PHP, Java, Python, ...)
POSIX NFA
Here, POSIX refers to the semantics of matching: how a regex behaves according to the POSIX standard, discussed later in this chapter. Do not confuse this with POSIX features, the bracket expressions specified by the same standard (see POSIX character-class bracket expression: [[:alpha:]]). Many programs support POSIX features without being POSIX engines.
Older, relatively simple programs such as egrep, awk and lex usually had electric DFA power. For them, the new standard simply confirmed the status quo, requiring no radical changes. However, there were gasoline-powered versions that needed adaptation for POSIX compliance. Combustion engines meeting California emissions rules, our POSIX NFAs, have excellent emissions but have become fragile through the required adjustments. They once ran with slightly misadjusted electrode gaps but now tolerate little deviation. Fuel once good enough now causes knocking. Yet with proper care, these engines run smoothly. And cleanly.
From the Department of Redundancy Department
Please revisit the motor analogy at the start of the chapter. Every sentence relates to regular expressions. A second reading also raises questions. What does it mean for an electric DFA simply to run? What affects a gasoline NFA? How do I maintain an NFA? What needs attention in a low-emission POSIX NFA? In the world of regular expressions, what is an engine that no longer runs?
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