XML technologies / Regular expressions / The art of writing regular expressions

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

With regex-directed matching in an NFA, as used by Perl, most Java packages, the .NET languages, Python and PHP, among others (see the table Programs and their regex engine types), tiny regex changes can drastically affect matching. Things irrelevant to a DFA become critical. NFA fine-tuning lets you construct regexes almost artistically, but the many options can also confuse. This chapter helps you learn that art.

The cornerstones are correctness and efficiency: finding exactly the match you want, no other, as quickly as possible. Under How regex engines work and Practical regex techniques we discussed writing correct regexes. Here we examine NFA efficiency and exploiting its characteristics. DFAs are mentioned where relevant, but this chapter primarily concerns NFAs. Fundamentally, we must understand backtracking and its consequences, learning to avoid it where possible. Understanding internals also gives us confidence with complex expressions.

First, a detailed example shows why these issues matter. A review of backtracking from How regex engines work, now focusing on efficiency, prepares for advanced techniques. Next come common internal optimizations that strongly affect efficiency, and how to benefit from tools implementing them. Finally, I present fast techniques that can give even NFAs rocket power.

Comparisons and backtracking

The examples illustrate common regex situations. To examine efficiency, I often count matching comparisons. Matching ˹marty˼ against smarty takes six: first ˹m˼ versus s fails, then ˹m˼ matches m, ˹a˼ matches a, and so on. I usually count backtracking too: none here, unless you count restarting at the string’s second position as implicit backtracking.

Exact counts are not important themselves; they are simply more informative than many, few, better, or not too many. Writing NFA regexes is not an exercise in counting comparisons and backtracking. These counts merely compare the examples’ quality.

Counts also vary by tool and version. Orders of magnitude and relative performance matter, not exact numbers. Account for built-in optimizations: a smart implementation may detect before matching that the regex cannot succeed, perhaps because a required character is absent. We will examine these optimizations later, but the general lesson matters more than individual cases.

Traditional NFA and POSIX NFA

Keep the tool’s engine type, traditional or POSIX NFA, in mind: some efficiency arguments apply only to one type. A change with no visible effect in one can strongly affect the other. Understanding the fundamentals helps you assess such situations.

  

  

 

 

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