XML technologies / Regular expressions / The art of writing regular expressions / Unrolling the loop / A recipe for unrolling loops

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

Putting it all together gives ˹"[^\\"]*(\\.[^\\"]*)*"˼ for quoted strings allowing escapes. It matches and rejects exactly the same strings as the earlier alternation version, but the unrolled loop version finishes within our lifetime. It is more efficient and avoids interminable matching.

The general pattern is:

˹öffnend normal*(speziell normal*)* schließend˼

 

Avoiding interminable matching

Three rules of thumb ensure ˹"[^\\"]*(\\.[^\\"]*)*"˼ never causes interminable matching:

1. speziell and normal must start differently

The subexpressions speziell and normal must not match at the same position. Our example obeys this rule. Normal is ˹[^\\"]˼ and speziell is ˹\\.˼. They cannot match the same string: the second requires an initial backslash, while the first forbids it.

In contrast, both ˹\\.˼ and ˹[^"]˼ can match starting at ›"Hallo▵\n"‹, making them unsuitable as speziell and normal. If both can start at one position, the path becomes ambiguous, introducing nondeterminism and interminable matching. The ›m a k u d o n a r u d o‹ example under Exponential matching illustrates this. A POSIX NFA, or a traditional NFA on failure, must try every permutation. That would defeat the whole purpose of rewriting the regex.

Ensuring speziell and normal never match at the same point makes speziell a checkpoint stopping possible nondeterminism in ˹(...)*˼. Otherwise, different iterations of ˹(...)*˼ could compete for the same text. Only one sequence of speziell and normal remains instead of thousands of partitions. Testing one sequence is much faster.

2. speziell must match at least one character

Second, ensure speziell cannot match nothing: it must consume at least one character. Otherwise, successive characters could match different star iterations in ˹(speziell normal*)*˼, recreating the (...*)* problem.

Choosing ˹(\\.)*˼ for speziell breaks this rule. Applying the notorious ˹"[^\\"]*((\\.)*[^\\"]*)*"˼ to ›Tubby‹ forces every permutation of multiple ˹[^\\"]*˼ against ›Tubby‹ before proving failure. Since speziell can match zero characters, it is not a genuine checkpoint.

3. speziell must be atomic

Text matched by one application of speziell must not be rematched by another. Consider recognizing empty space in Pascal: whitespace or comments, {...} in Pascal. The regex for comments is ˹\{[^}]*\}˼, and the entire interminable regex is ˹(\{[^}]*\}|●+)*˼. We might identify speziell and normal as:

speziell

normal

˹●+˼

˹\{[^}]*\}˼

Inserted into our ˹normal*(speziell normal*)*˼ recipe, this yields ˹(\{[^}]*\})*(●+(\{[^}]*\})*)*˼. How does it match ›{Kommentar}●●●{noch●einer}●‹? A run of spaces matches in many ways: one ˹●+˼, many ˹●+˼ matching one space each, or any combination. This is exactly like ›m a k u d o n a r u d o‹.

The root problem is that speziell can match a small piece of text that it can also match as part of a larger piece. Grouping with (...)* permits many ways to match the same text, opening a Pandora’s box of nondeterminism.

On success, the first attempt, with one ˹●+˼ consuming all spaces, will probably work. On failure, possible only when this regex is part of a larger one, every permutation of ˹(●+)*˼ against every space sequence must be tried. This takes time without success. Since speziell was supposed to be the checkpoint, nothing stops its own nondeterministic behavior.

Choose a speziell matching an exact number of characters. At least one space is required here, so use ˹●˼ and leave further spaces to the enclosing ˹(...)*˼ . This illustrates the idea, but in practice I would swap speziell and normal and use

˹●*(\{[^}]*\}●*)*˼

. Pascal usually has more whitespace runs than comments, so treating the common case as normal is more efficient.

 

General warning signs

Understanding these rules, perhaps through repeated reading and experience, helps spot potential interminable matching. Nested quantifiers such as ˹(...*)*˼ are warning signs, although some legitimate regexes use them:

  • ˹(Re:●*)*˼ finds repeated ›Re:‹ in a subject line, useful for removing an ugly run such as ›Subject:●Re:●Re:●Re:●Hallo!‹.

  • ˹(●*\$[0-9]+)*˼ matches dollar amounts optionally separated by whitespace.

  • ˹(.*\n)+˼ matches one or more lines. If dot matches newline and a later failure occurs, however, this becomes the prototype of interminable matching!

These examples are safe because each has a checkpoint. There are no multiple ways to match the same text, so Pandora’s box stays shut. In the first, the checkpoint is ˹Re:˼, in the second ˹\$˼, and in the third ˹\n˼, again assuming dot does not match newline.

  

 

 

 

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