XML technologies / Regular expressions / The art of writing regular expressions / Programming techniques for faster expressions / Emulating the first-character optimization

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

If the program does not use the first-character optimization, it can often be emulated with an appropriate lookahead (see Lookahead and lookbehind) at the start of the regex. Lookahead checks in advance whether the character after the current position is one required by the full regex that follows.

For example, with ˹Jan|Feb|...|Dez˼, ˹(?=[JFMASOND])(?:Jan|Feb|...|Dez)˼ can check in advance whether the current character is in ˹[JFMASOND]˼, the initial letters of month names. Use this technique carefully: its overhead quite often exceeds the possible gain. This example improves performance on most systems I tested (Java, Perl, Python, Ruby, and the .NET languages). Apparently, none recognizes this optimization opportunity by itself.

PCRE normally does not use this optimization, but it can be enabled with pcre_study (in PHP this is the s modifier). Tcl naturally does this correctly on its own; see the box DFAs, Tcl, and regex tuning.

If the regex engine can compare the first character against ˹[JFMASOND]˼ through an internal optimization, that is naturally much faster than our explicit lookahead. Can we rewrite the regex so it performs this test internally? On many systems, this thoroughly contorted version does work:

˹[JFMASOND](?:(?<=J)an|(?<=F)eb|...|(?<=D)ez)˼

You probably will not understand it at first glance; take your time, as it is a useful exercise. The simple initial character class allows most systems to apply the first-character optimization: the transmission tests the first character before starting the regex.

If the subject string contains few characters from this class, the result is much faster than the original ˹Jan|...|Dez˼ or the earlier lookahead version. If many such characters occur, however, the extra cost of the individual lookbehinds can cancel the gain. This version is also considerably harder to understand. It remains an interesting and instructive exercise.

 

Do not do this with Tcl!

In some cases, a manually optimized regex can thus be less efficient than the original. The box DFAs, Tcl, and regex tuning explains that Tcl’s regex engine is largely insensitive to expression form, making most manual optimizations ineffective. However, form does matter in the month-name example. In my tests, placing the lookahead ˹(?=[JFMASOND])˼ first made the regex more than a hundred times slower.

 

Do not do this with PHP!

As mentioned, PHP’s s modifier enables the study option, which applies this optimization automatically. In PHP this is explained in more detail under The S modifier.

  

 

 

 

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