XML technologies / Regular expressions / How regex engines work / More on greediness / Taking advantage of ordered alternation

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

Returning to the example ˹(\.\d\d[1-9]?)\d*˼ from Greedy or lazy—the match comes first. ˹\.\d\d[1-9]?˼ really means “either ˹\.\d\d˼ or ˹\.\d\d[1-9]˼”, so we could write the entire expression using alternation: ˹(\.\d\d|\.\d\d[1-9])\d*˼. There is no compelling reason to do so; it is just an example. Is this really the same as ˹(\.\d\d[1-9]?)\d*˼? With greedy alternation, yes; with ordered alternation, the two versions differ substantially.

First consider ordered alternation. If the first branch matches, ˹\.\d\d˼, then the following ˹\d*˼ certainly matches too. This might also be a third nonzero digit, which, remember, we want to capture inside the parentheses. Moreover, the first part of the second branch is an exact copy of the entire first branch. If the first fails, the second must fail too. The regex engine will try, but is certain to encounter another local failure.

If we reverse the alternatives with ˹(\.\d\d[1-9]|\.\d\d)\d*˼, we initially just get a copy of the original ˹(\.\d\d[1-9]?)\d*˼. But the alternation is meaningful: now the second branch can sometimes match when the first fails. It is still ordered alternation, but we have arranged the branches to try the longest possible match first, as greedy behavior would do automatically.

Splitting the expression into two branches with ˹[1-9]?˼ and putting the shorter first created a sort of lazy question mark. Here, that is irrelevant, since the second branch cannot match if the first fails. I often see this kind of false alternative; using it with a traditional NFA always reflects a misunderstanding. One book used ˹a*((ab)*|b*)˼ to explain parentheses in a traditional NFA. A rather silly example, is it not? The first branch, ˹(ab)*˼, can never fail, making all subsequent branches—only ˹b*˼ in this case—irrelevant. Adding

˹a*((ab)*|b*|.*|Fischers●Fritz●fischt●frische●Fische|[a-z])˼

would give exactly the same result. The lesson: when several ordered alternatives can match the same text, consider their order carefully.

 

Pitfalls of ordered alternation

Clever regex design can often exploit lazy alternation to obtain exactly the desired matching behavior. But it also has pitfalls. Suppose we want to recognize a January date in English notation, such as ›Jan 31‹. We need something more sophisticated than ˹Jan●[0123][0-9]˼, which accepts dates such as ›Jan 00‹ and ›Jan 39‹ but misses dates such as ›Jan 7‹.

The easiest approach is to break the problem into parts. For the first through ninth, use ˹0?[1-9]˼ , allowing an optional leading zero. With ˹[12][0-9]˼ we cover 10 through 29, and with ˹3[01]˼ the remaining two. Combining them gives ˹Jan● (0?[1-9]|[12][0-9]|3[01])˼.

How do you think this matches ›Jan 31 is dad's birthday‹? Naturally, we expect ›Jan 31‹, but ordered alternation recognizes only ›Jan 3‹. Surprised? In the first branch, ˹0?[1-9]˼ the ˹0?˼ does not match, but the branch succeeds because the following ˹[1-9]˼ matches the 3 of 31 . Having reached the end of the regex, matching is complete.

A different order, or greedy alternation, would avoid the problem. This version works: ˹Jan●([12][0-9]|3[01]|0?[1-9])˼.

Another approach is ˹Jan●(31|[123]0|[012]?[1-9])˼. As with the first version, branch order is crucial. The third version, however, ˹Jan● (0[1-9]|[12][0-9]?|3[01]?|[4-9])˼ works regardless of branch order. Comparing these three expressions can be instructive. I leave that exercise to you; the following box may help.

 

Ways to divide calendar dates

Some approaches to recognizing calendar dates. The calendar tables shade dates to match their corresponding alternatives in the regex.
Ways to divide calendar dates

  

 

 

 

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