XML technologies / Regular expressions / The art of writing regular expressions / Programming techniques for faster expressions / Exposing literal text
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Many internal optimizations depend on whether the regex engine can determine that a match must contain some literal text. Some engines do this better than others. Making literal text clearer to the regex engine makes optimization opportunities easier to recognize.
Factoring out a quantifier’s minimum
Writing ˹xx*˼ instead of ˹x+˼ makes it clear that at least one ›x‹ must occur in every match. Similar reasoning transforms ˹-{5,7}˼ into ˹------{0,2}˼.
Factoring common parts out of alternation
Replacing ˹(?:klipp|klar)˼ with the factored form ˹kl(?:ipp|ar)˼ helps the engine recognize that every match must contain ›kl‹. Factoring on the right is also possible, for example with ˹(?:Optim|Standardis)ierung˼. Factoring out a line anchor this way is even better.
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