XML technologies / Regular expressions / How regex engines work / Recap
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
If you understood everything in this chapter on the first reading, you probably already knew something about regular expressions. This chapter was tough, to put it mildly. Understanding it took me some time, and truly understanding it took even longer. I hope a concise but complete presentation makes it easier. I tried to keep the examples clear without oversimplifying them, a common trap that ultimately hinders real understanding. This chapter is densely packed, so the following recap includes many page references.
There are two basic operating models for regex engines: regex-directed NFAs (see NFA engine: regex-directed) and text-directed DFAs (see DFA engine: text-directed). The expanded abbreviations appear in An initial comparison of NFA and DFA engines.
Together with the effects of the POSIX standard (see POSIX and the leftmost-longest match), this gives three engine types for practical purposes:
Traditional NFA: a highly tuned gasoline engine
POSIX NFA: a standards-compliant gasoline engine
DFA, POSIX or otherwise: an electric motor without quirks
To exploit a tool fully, know its engine type and adapt the regex accordingly. Traditional NFAs are the most common, followed by DFAs. The table Programs and their regex engine types lists common tools and their engine types. In Identifying the regex engine type I show how to identify a tool’s engine yourself.
The overriding rule for all engine types is that a match starting earlier in the string takes priority over one starting later. This follows from the transmission trying the regex anew at each string position (see The transmission advances to the next character).
For an attempt starting at a particular position:
- DFA: text-directed engines
DFAs find the longest possible match, period. That is all; thank you for the conversation (see The leftmost-longest match). They are consistent and very fast, and discussing them is boring (see Speed and efficiency)
- NFA: regex-directed engines
NFAs must work their way through matching. The heart of an NFA is Backtracking (see Backtracking, Backtracking and greediness). Metacharacters control matching: quantifiers such as star and plus are greedy (see Rule 2: ordinary quantifiers are greedy). Alternation is ordered (see Is alternation greedy?), but greedy in POSIX NFAs.
- POSIX NFA
It must find the longest possible match, period. It is still not boring, because efficiency must be considered, the subject of ( The art of writing regular expressions).
- Traditional NFA
The most expressive type of regex engine , because regex control can find precisely the match of interest.
Understanding the concepts and methods in this chapter is the foundation for writing correct, efficient regular expressions, precisely the subject of Practical regex techniques and The art of writing regular expressions.
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