XML technologies / Regular expressions / The art of writing regular expressions / Programming techniques for faster expressions / Guiding the regex engine to a match
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
A programming principle that makes a substantial difference with NFA engines is to move the controlling parts of the regex as far right as possible. We have already seen an example: ˹kl(?:ipp|ar)˼ instead of ˹klipp|klar˼. In the second case, alternation encompasses the entire regex; in the first, the relatively expensive alternation control structure is used only after an ˹kl˼ has been found.
Under Unrolling the loop we will encounter a more refined version of this principle, but first let us consider some simpler cases.
Put the most likely alternative first
This book describes several situations where alternative order is crucial to how the regex works (see Metacharacters, Pitfalls of ordered alternation, Understanding the context, Disabling the transmission). In those cases, of course, the order cannot be changed: correctness comes before optimization. In other cases, order does not matter, and efficiency can improve by putting the alternatives most likely to match first.
In the hostname regex (see Checking a hostname’s syntax), the final part listed top-level domains in an alternation. They could simply be listed alphabetically, as in ˹(?:aero|biz|com|coop|...)˼. However, some domains near the beginning of the alphabet are uncommon. Moving common domains to the front could be substantially more efficient: ˹(?:com|edu|org|net|...)˼. (Translator’s note: In German-speaking countries, it is also advantageous to list the country codes for Germany, Austria, and Switzerland explicitly at the front of the alternation, as in ˹(?:com|de|at|ch|edu|...|[a-z][a-z])˼; otherwise, they would be recognized only by the two character classes at the very end.)
Naturally, this matters only with a traditional NFA, and even then only when a match is found. With a POSIX NFA, or on failure, all alternatives must be tried anyway, so their order is irrelevant.
Do not factor out the end of an alternation
Continuing the hostname example, compare ˹(?:com|edu|...|[a-z][a-z])\b˼ with ˹com\b|edu\b|...\b|[a-z][a-z]\b˼. In the second case, the ˹\b˼ following the alternation has been distributed over all alternatives. If a branch would match without the word boundary, this can reveal sooner, without leaving the alternation, that the entire regex cannot match; the engine stays within the alternation and tries the next branch. This saves the overhead of leaving the parentheses and resuming the alternation after a later failure.
This may not be the best illustration, since it helps only when the alternative matches but the following parts of the regex fail. We will encounter the technique again near the end of the chapter; watch for the discussion of $SONST* under A well-guided regex is a fast regex.
This optimization can be dangerous
This manual optimization risks defeating a better automatic optimization inside the engine. If the subexpression distributed over the alternatives is literal text, such as the colon in ˹(?:klipp|klar):)˼, which we transform into ˹klipp:|klar:˼, this runs counter to the technique recommended under Exposing literal text. The optimizations described there are generally more effective, so take care not to prevent them.
A similar warning applies to the end-of-line anchor ˹$˼ on systems that optimize it (see Factor out $ at the end of the expression). On such systems, ˹(?:com|edu|...)$˼ is much faster than ˹com$|edu$|...$˼, where the dollar sign is distributed over all alternatives. (However, among the systems tested, only Perl uses this optimization.)
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