XML technologies / Regular expressions / The art of writing regular expressions / Programming techniques for faster expressions / Splitting into multiple regular expressions

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

In some cases, many small expressions can be much better than one large, complicated regex. Searching for month names, for example, may be faster with separate tests for ˹Januar˼, ˹Februar˼, ˹März˼ and so on than with one large alternation ˹Januar|Februar|März|...˼. No literal string occurs in every alternative of the latter, preventing the corresponding optimization (see Optimizing literal strings within text). Testing the single regex containing all month names is therefore rather slow at each position in the subject string.

Around the time I wrote this section, I encountered just such a problem. Working with a Perl database module, I noticed my client wrongly sending queries containing strings such as ›HASH(0x80f60ac)‹ instead of the hash element’s contents. I wanted the module to detect such incorrect data and issue an error. I planned to use a regex like ˹\b(?:SCALAR|ARRAY|...|HASH)\(0x[0-9a-fA-F]+\)˼ .

Efficiency mattered greatly here. Is this regex fast? Perl’s regex debugging mode reveals information about optimizations (see Regex debugging information). I wanted to check whether Searching for single characters or fixed substrings would be applied: a good implementation should realize every match must contain ›(0x‹ . I also knew that ›(0x‹ almost never occurred in my data, so this optimization should nearly always lead to a real match. Unfortunately, Perl missed the opportunity and tried every alternative at every character, which was unacceptably slow.

Since I was investigating optimizations, making this regex faster was an interesting challenge. One attempt was the rather complicated ˹\(0x(?<=(?:SCALAR|...|HASH)\(0x)[0-9a-fA-F]+\)˼. The idea is first to search for ˹\(0x˼ and then use positive lookbehind to ensure one of the alternative keywords immediately precedes it. These contortions were intended to make the regex engine first search for the literal string ˹\(0x˼ and thus work more efficiently. The optimization Searching for single characters or fixed substrings and also the first-character optimization could then be applied (see first-character optimization). This regex would probably be very fast, but Perl unfortunately supports only fixed-length lookbehind (see Lookahead and lookbehind), so I had to start over.

If Perl would not automatically check for the literal ˹\(0x˼ in the subject string, I could do so manually:

if ($data =~ m/\(0x/
    and
    $data =~ m/(?:SCALAR|ARRAY|...|HASH)\(0x[0-9a-fA-F]+\)/)
{
   # Warnung über ungültige Daten ausgeben ...
}

The test for ˹\(0x˼ excludes almost every line. Only the few containing it undergo the large, relatively slow regex. The solution is both very fast and readable. (Note: you can verify this yourself. The module is DBIx::DWIW, available on CPAN. It provides easy MySQL access and was developed by Jeremy Zawodny and me at Yahoo!.)

  

 

 

 

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