XML technologies / Regular expressions / How regex engines work / More on greediness / Lazy quantifiers

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

These problems arise because ordinary quantifiers are greedy. Some NFAs support lazy quantifiers (see Lazy quantifiers); *? is the lazy version of *. With a lazy ˹<B>.*?</B>˼ and the string

...<B>Millionen</B> und <B>Abermillionen</B> von Sonnen...

, it works as follows. After the ˹<B>˼ matches, the ˹.*˼ immediately decides it must match nothing, and the engine passes control to the following ˹<˼.

in the text: ›...<B>▵Millionen</B>...‹

in the regex: ˹<B>.*?▵</B>˼

This ˹<˼ fails at this position, so Backtracking returns control to the lazy ˹.*?˼. It still has other possibilities: it can match more than nothing. It now matches ...<B>Millionen..., and ˹<˼ again tries to match the next character. This fails, and the cycle repeats ten times until the following match is obtained: ˹.*?˼ Millionen . Then the following ˹<˼ matches, along with the entire subexpression ˹</B>˼. We have found a match:

...<B>Millionen</B> und <B>Abermillionen</B> von Sonnen...

Greedy quantifiers can be extremely useful, but can also get in the way. Lazy versions are ideal in some situations, making possible things otherwise very difficult or impossible. Yet I often see inexperienced programmers use lazy quantifiers in unsuitable situations. Even the solution just shown can be wrong in some cases. What happens if we apply ˹<B>.*?</B>˼ to the following text?

...<B>Millionen und <B>Abermillionen<B> von Sonnen...

This produces the underlined match, probably not the expected one. Nothing in ˹.*?˼ prevents matching <B> too.

This is a classic example of why a lazy quantifier is often a poor substitute for a negated character class. In the example using ˹".*"˼, the negated class ˹[^"]˼ explicitly forbids matching any delimiter, unlike a dot. Our current regex cannot do that.

A negative lookahead, if supported (see Lookahead and lookbehind), lets us build an equivalent of a negated class. Alone, the expression ˹(?!<B>)˼ checks that no <B> occurs at the current position. We want the dot in ˹<B>.*?</B>˼ to match only there. Replacing the dot with ˹((?!<B>).)˼ gives a regex whose dot matches any character unless forbidden by the lookahead. The complete regex is rather confusing, so I use free-spacing mode (see Free-spacing mode) with comments:

<B>                # Passt auf das <B> am Anfang ...
(                  #     So viel vom Folgenden wie nötig ...
  (?!  <B>  )      #        Wenn wir kein <B> haben ...
  .                #             ... ist jedes Zeichen recht.
)*?                #
</B>               # ... bis das schließende Begrenzungszeichen passen kann.

A small adjustment to the lookahead also lets us use an ordinary greedy quantifier, which many find easier to understand:

<B>                # Passt auf das <B> am Anfang ...
(                  #     So viel vom Folgenden wie möglich ...
  (?!  </?B>  )    #        Wenn wir weder <B> noch </B> haben ...
  .                #             ... ist jedes Zeichen recht.
)*                 # (jetzt gierig)
</B>               # ... bis das schließende Begrenzungszeichen passen kann.

The lookahead prevents the part between delimiters from matching </B> or <B>. This solves the problem we first tried to address with lazy quantifiers, so an ordinary greedy quantifier can be used. This regex can be improved further; we return to it in The art of writing regular expressions (see Examples of unrolling loops).

  

 

 

 

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