XML technologies / Regular expressions / The art of writing regular expressions / Unrolling the loop / Observations

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

Considering the whole discussion of quoted strings, I see substantial advantages in the modified regex, but also some disadvantages.

Disadvantages

Readability: The biggest disadvantage is probably that the original expression ˹"([^\\"]|\\.)*"˼ is easier to understand. We sacrificed some readability for efficiency.

Maintainability: ˹"[^\\"]*(\\.[^\\"]*)*"˼ is slightly harder to maintain. When the regex changes, care must be taken to keep both ˹[^\\"]˼ identical.

Advantages

Speed: The new regex also works well when no match exists, or when used with a POSIX NFA. Because each regex part matches only a specific character sequence, the engine can quickly determine when the text cannot match.

Faster still: The regex flows well, borrowing a term from The free-flowing regex. In my benchmarks with traditional NFAs, the unrolled version was consistently faster than the old alternation version, even when the text matched and the old version came nowhere near endless 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