XML technologies / Regular expressions / How regex engines work / Start the engine! / Identifying the regex engine type

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

Since some features belong to only one engine type, and engines differ in edge cases, a few test expressions can often identify the type. Otherwise, the choice would not matter. You may not yet understand why these tests behave as they do, but I include them in case your program is absent from table Programs and their regex engine types.

Traditional NFA or not?

The traditional NFA is the most widespread and is usually easy to identify. Does it support lazy quantifiers (see Lazy, non-greedy quantifiers)? If so, it is almost certainly traditional NFA. Lazy quantifiers are impossible in a DFA and conflict with the standard in a POSIX NFA. To be certain, apply regex ˹nfa|nfa●not˼ to string ‘nfa●not’. If the match is ‘nfa’, it is traditional NFA. If the entire string ‘nfa●not’ matches, it is POSIX NFA or DFA.

DFA or POSIX NFA?

Distinguishing POSIX NFA from DFA is usually just as easy. DFAs do not support capturing groups or backreferences, which provides a clue. Some hybrids, however, normally use DFA but switch to NFA for those constructs.

This test reveals a great deal. Apply regex ˹X(.+)+X˼ to a string such as ›=XX======================‹, here using egrep:

echo =XX========================================= | egrep 'X(.+)+X'

A slow result indicates NFA; if the preceding test excluded traditional NFA, it is probably POSIX NFA. An immediate result indicates DFA or a sophisticated NFA whose optimizer recognizes the situation. A warning such as “Stack overflow” or another abort means NFA.

  

 

 

 

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