XML technologies / Regular expressions / The art of writing regular expressions / Unrolling the loop / Using atomic groups and possessive quantifiers

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

Our original regex with interminable matching really had trouble only on failure. Successful matching is fairly fast because the subexpression ˹[^\\"]+˼ (the normal part) handles most of the subject string. Since ˹[...]+˼ is usually well optimized (see Regex engine optimizations) and consumes most of the text, the relatively slow alternation and outer quantifier ˹(...)*˼ have little left to do.

The problem is that ˹"(\\.|[^\\"]+)*"˼ stalls on failure because backtracking repeatedly tests the same useless saved states. We know they are useless because endless permutations keep testing the same thing. (If ˹abc˼ does not match ›foo‹, neither will ˹abc˼ or ˹abc˼, let alone ˹abc˼, ˹abc˼ or ˹abc˼.) Removing these saved states would let the engine detect failure much sooner.

Two techniques remove or ignore these states: atomic groups or possessive quantifiers.

Before discussing backtracking and its avoidance, let us reverse the alternatives, from ˹"(\\.|[^\\"]+)*"˼ to ˹"([^\\"]+|\\.)*"˼, putting the normal part first. As repeatedly noted, order matters when alternatives may match the same text. Here they are mutually exclusive, so we can choose whichever order is clearest or fastest.

 

Avoiding interminable matching with possessive quantifiers

Our interminable regex ˹"([^\\"]+|\\.)*"˼ has two quantifiers. We could make either or both possessive. Does it matter? The principal backtracking problems come from states saved by ˹[...]+˼, so I first made that quantifier possessive. The regex then fails quickly. Alternatively, making ˹(...)*˼ possessive discards every state created inside the group: those from the plus group and those from alternation. Could that be better?

No decision is needed because both can be possessive. Which of the three versions is fastest probably depends heavily on optimization. Since only Sun’s Java regex package implements possessive quantifiers, comparisons are limited. Tests gave good results for all three versions. Sun apparently optimized possessive quantifiers very well.

 

Avoiding interminable matching with atomic groups

Replacing the ordinary parentheses in ˹"([^\\"]+|\\.)*"˼ with atomic ones is tempting: ˹"(?>[^\\"]+|\\.)*"˼. But understand that ˹(?>...|...)*˼ differs greatly, in which states it discards, from the possessive ˹(...|...)*+˼ in the preceding section.

With possessive ˹(...|...)*+˼, no saved states remain after the subexpression matches. The atomic group ˹(?>...|...)*˼, however, removes only states resulting from the alternatives and alternation itself. The star lies outside the atomic construct, so later backtracking can still reach all skip-this-option states. Avoiding those too requires another atomic group, such as ˹(?>(...|...)*)˼. Only that does the same job as possessive ˹(...|...)*+˼ from the previous section.

˹(...|...)*+˼ and ˹(?>...|...)*˼ are excellent solutions to interminable matching, but discard different states. For more on similarities and differences, see Faster failures with atomic groups.

  

 

 

 

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