XML technologies / Regular expressions / How regex engines work / Pattern-matching basics / Parts of the regex engine

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

A motor consists of many parts, and you cannot understand how it works without knowing its main components. For regular expressions, these are the basic units—literals, quantifiers such as star and question mark, character classes, parentheses, and so on—described under Features and dialects (see the table Constructs covered in this section). Their combination and the way the engine uses them make a regex what it is, so we must examine how the parts fit and interact. They are:

Literals (e.g. a \* ! 枝 ...)

For a literal character such as ˹z˼ or ˹!˼, matching simply asks whether the literal equals the current string character. If the entire regex consists of literals, as in ˹usa˼, it is treated as “˹u˼ and then ˹s˼ and then ˹a˼”. Ignoring case makes it only slightly more complicated: if ˹b˼ must also match B, or vice versa. (With Unicode there are additional complications; see Case-insensitive mode.)

Character classes, dot, Unicode properties, and so on

Finding a match with a character class, dot, Unicode property, or similar construct (see Character classes and related constructs) is not much harder. No matter how many characters a class contains, it matches just one string character. (Strictly speaking, POSIX collating sequences can match multiple characters, as discussed in Features and Dialects, but that is exceptional. Some Unicode characters can also match multiple characters in case-insensitive mode (see Case-insensitive mode), although most implementations do not support this.) Dot simply abbreviates a large character class containing all or almost all characters (see Dot-matches-all mode), so matching is also simple. The same applies to the shorthands ˹\w˼, ˹\W˼ and ˹\d˼.

Capturing parentheses

Parentheses used only to capture previously matched text, rather than for grouping, do not affect the matching process.

Anchors (e.g. ˹^˼ ˹\Z˼ ˹(?<=\d)˼ ...)

There are two kinds of anchors: simple ones (^, $, \G, \b, ...; see Anchors and other zero-width assertions) and more complex ones, lookahead and lookbehind (see Lookahead; lookbehind). Simple anchors require a simpler matching process, checking only a particular position in the string (^, \Z, ...) or comparing two adjacent characters (\<, \b, ...). Lookaround constructs can contain arbitrarily complicated subexpressions, so their matching behavior can also be very complex.

No electric capturing parentheses, backreferences, or lazy quantifiers

I intended to focus first on what regex engines share, but cannot resist a preview. Capturing parentheses, and therefore backreferences and the functionality of $1, are like fuel additives: they exist only for NFA combustion engines and make no sense for electric motors. Lazy quantifiers are similar. A DFA’s operation simply does not allow backreferences to capturing parentheses. (There are hybrids combining the best of both types, discussed in the box DFA speed with NFA features: regex nirvana?.) This explains why DFA-based tools do not offer these features. Neither awk, lex nor egrep has backreferences or an equivalent of $1.

You may nevertheless have noticed that egrep in GNU’s version still supports backreferences. It can do so because it has two engines under the hood! GNU egrep first uses a DFA to find patterns. On finding a match, it applies an NFA that understands backreferences. Later in this chapter, we will see why DFAs cannot handle backreferences and why anyone uses such restricted engines. DFAs have advantages, including very fast matching.

  

 

 

 

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