XML technologies / Regular expressions / How regex engines work / More on greediness / Greedy or lazy: a match takes priority

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

Recall the stock-price correction example in Extended introductory examples (see Code example: stock prices). We will return to this example several times, so here is a recap. Limited floating-point precision produced values such as “1.62500000002828” and “3.00000000028822” instead of “1.625” and “3.00”. To correct this, I used

$kurs =~ s/(\.\d\d[1-9]?)\d*/$1/;

to truncate the value in $kurs to two or three decimal places. ˹\.\d\d˼ matches the first two decimal places; ˹[1-9]?˼ matches a third only if it is nonzero.

I wrote:

What has matched so far must be preserved, so parentheses capture it in $1. We will use this $1 in the replacement text. If the regex matched only this part, the matched string would be replaced with itself: not very interesting. But our regex continues after the parentheses. If this part of the regex also matches characters, they are not saved in $1 and do not appear in the replacement, so they are deleted. Here, these are all the additional digits matched by ˹\d*˼ at the end of the regex.

So far so good, but what if the value of $kurs is already correct? If it is 27.625, then ˹(\.\d\d[1-9]?)˼ matches everything from the decimal point through the end of the string. The ˹\d*˼ at the end matches nothing, so the substitution replaces .625 with .625: a no-op.

That is the desired result, but would avoiding substitution altogether not be better in this case? Replacement is needed only if the ˹\d*˼ at the end matches one or more digits. One or more digits—that means plus! We simply replace ˹\d*˼ with ˹\d+˼:

$kurs =~ s/(\.\d\d[1-9]?)\d+/$1/

This works as before for awkward numbers such as “1.62500000002828”. But for already-correct numbers such as “9.43”, the final ˹\d+˼ no longer matches, and no substitution occurs. A good improvement, right? No! What happens with a number having three decimal places, such as 27.625? We want this value to remain unchanged, but unfortunately that is not what happens. Try tracing the engine’s behavior with 27.625 yourself, paying particular attention to how the ›5‹ and regex influence one another.

In retrospect, the problem is simple. Start where ˹(\.\d\d[1-9]?)\d+˼ has matched the string 27.625, and observe that ˹\d+˼ fails. This is no problem for the engine: the match of ˹[1-9]˼ with ›5‹ was optional, with a saved state available. Taking that path lets ˹[1-9]?˼ match nothing, and the 5 is consumed by the required at-least-once behavior of ˹\d+˼. The whole expression therefore matches, but incorrectly: .625 is replaced with .62, making the value in $kurs incorrect.

What if we had used the lazy version of ˹[1-9]?˼? We would get the same match, but without temporarily matching and giving back the 5, since ˹[1-9]??˼ first tries to match nothing. Lazy quantifiers are therefore no solution here.

  

 

 

 

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