XML technologies / Regular expressions / How regex engines work / Pattern-matching basics / Rule 1: the leftmost-starting match wins

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

This rule prefers a match starting earlier in the string over another starting farther right. It says nothing about match length, which comes later, only that the leftmost-starting possible match wins. Several matches may start at the same position, so strictly it should say “One of the leftmost-starting matches wins”, but that sounds awkward.

The first attempt starts immediately before the first character. Every possible combination of the potentially complex regex is tried there. If all permutations fail, a completely new attempt applies the entire regex immediately before the second character. This repeats at each position until a match is found. Failure is returned only if every attempt at every position, including just after the final character, fails.

Applying ˹ORA˼ to ANORAK first fails at the start, because ˹ORA˼ does not match ANO. The second attempt fails too, since NOR does not match. The third succeeds, and the regex engine returns ANORAK.

Without this rule, results may surprise you. Searching for ˹der˼ in

Das Federvieh war der Lebenszweck der Witwe Bolte

finds a match inside Federvieh, before the word der. That word could match, but Federvieh appears earlier, so the match is reported there. For a program such as egrep this does not matter: egrep cares only whether a match exists, not where it occurs. Other applications, such as search and replace, do care.

A simple question: what does ˹Bolte|Leben|Feder|Witwe˼ match in ›Das Federvieh war der Lebenszweck der Witwe Bolte‹? The answer is here:

 

Answer

Remember that the regex is tried in full at every position. Thus ˹Bolte|Leben|Feder|Witwe˼ matches Feder in ›Das Federvieh war der Lebenszweck der Witwe Bolte‹, rather than Bolte, even though Bolte is the first alternative.

The regex could match Bolte and the other words, but those are not the earliest possible matches, so they are not found. All permutations of the whole regex are tried before moving to the next position. Here, every alternative—˹Bolte˼, ˹Leben˼, ˹Feder˼, and ˹Witwe˼—is tested before advancing through the string.

 

The transmission advances to the next character

Think of a car’s transmission delivering the engine’s power to the drive wheels. The engine does the real work by turning the crankshaft; the transmission passes that power along in the selected gear.

The transmission’s main job: advancing

If the regex engine finds no match at the start, the transmission normally advances to the next position, then the next, and so on. But if the regex begins with an anchor, for example, it may see that advancing is pointless because matching is possible only at the start. These and other internal optimizations are explained in The art of writing regular expressions.

  

 

 

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