XML technologies / Regular expressions / The art of writing regular expressions / Common optimizations / Transmission optimizations
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
If the regex engine cannot determine anything beforehand about possible matches themselves, it may still be able to reduce the number of positions where the regex must be tried.
Line- and string-anchor optimizations
If a regex begins with ˹^˼, the entire regex obviously needs testing only at positions where ˹^˼ matches.
The remarks under Searching for individual characters or fixed substrings about when the engine can apply the optimization also apply here. An implementation using this optimization should recognize that ˹^(klipp|klar)˼ can match only at positions where ˹^˼ also matches, but most systems cannot cope with the essentially equivalent ˹^klipp|^klar˼ . In that case, you can help the engine with ˹^(klipp|klar)˼ or, better still, with ˹^(?:klipp|klar)˼.
Similar optimizations exist with the anchor ˹\A˼ and, for repeated searches, with ˹\G˼.
Implicit line-anchor optimization
An engine with this optimization recognizes that a regex beginning with ˹.*˼ or ˹.+˼ and containing no top-level alternation might just as well have ˹^˼ at the beginning. The line-anchor optimization from the preceding section can then save considerable unnecessary work.
More sophisticated systems can apply this optimization even when the initial ˹.*˼ or ˹.+˼ is enclosed in parentheses. Capturing parentheses require caution, however: adding an implicit anchor at the start of ˹(.+)X\1˼ would miss the match ›1234X2345‹. (Note: Perl used this over-optimization for over ten years until Perl contributor Jeff Pinyan found and fixed the bug in early 2002. Apparently, regular expressions like ˹(.*)X\1˼ are uncommon, or the error would have been found sooner.)
End-of-string or end-of-line anchor optimization
This optimization concerns regular expressions ending in ˹$˼ or another line anchor (see End of line, end of string). Such a regex can match only at positions within a certain distance of the end of the string. For example, any possible match for ˹regex(es)?$˼ must begin eight or fewer characters from the end of the string. (Note: I say eight rather than seven because ˹$˼ can also match just before a final newline in some dialects.) The transmission can simply skip all earlier positions.
First-character optimization
This generalizes searching for individual characters or fixed substrings. It uses the same information—every possible match must begin at a particular character or literal substring—to direct the transmission straight to that position. For example, ˹klipp|klar|˼ can match only immediately before an ›k‹ or an ›g‹; the transmission need not try the regex at any other character. The longer the literal substring, the fewer false starting positions are used.
Optimization with embedded literal strings
This is almost the first-character optimization, but uses the distance to a literal substring in the subject text. For a match of ˹\b(perl|java)\.regex\.info\b˼, for example, the literal string ›.regex.info‹ occurs after exactly four characters. The transmission can therefore use a fast Boyer–Moore search to locate ›.regex.info‹ and start the actual regex engine four characters earlier.
This generally works only if the literal string has a fixed offset. In ˹\b(vb|java)\.regex\.info\b˼, there is a literal string, but it can occur anywhere within the match.
Transmission length detection
This optimization is closely related to Minimum-length check optimization. It lets the transmission determine that no match is possible near the end of the subject string and avoid further attempts.
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