XML technologies / Regular expressions / The art of writing regular expressions / Unrolling the loop / Method 1: a regex built from past experience
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
When analyzing ˹"(\\.|[^\\"]+)*"˼, examining a few strings and seeing which subexpressions are actually used is instructive. For ›"Hallo!"‹, for example, the expression actually used is just ˹"[^\\"]+"˼. The match used only the initial ˹"˼, one occurrence of the alternative ˹[^\\"]+˼, and the final ˹"˼. For
"Er sagte \"Hallo!\" und ging"
, it is ˹"[^\\"]+\\.[^\\"]+ \\.[^\\"]+"˼. The used subexpressions are marked here and in the table below. A custom regex for every subject string would be nice, but impossible. We can still look for common patterns and build a faster equivalent expression from them.
Table: examples of unrolling a loop.
Subject string | Regex actually used |
|---|---|
|
|
|
|
|
|
|
|
| |
|
|
|
|
Focus first on the table’s first four examples. The underlined parts match “an escaped character followed by some ordinary characters”. In every case, the expression between the quotes begins with ˹[^\\"]+˼, followed by a series of ˹\\.[^\\"]+˼ sequences. Written as a regex, this is ˹[^\\"]+(\\.[^\\"]+)*˼. This specific case illustrates a frequently useful general principle.
A general approach to unrolling loops
For quoted strings, the quote and any escaped characters are “special”: the quote ends the string, and a backslash says the following character does not. Everything else, namely ˹[^\\"]˼, is “normal”. Examining how these characters combine into ˹[^\\"]+(\\.[^\\"]+)*˼ reveals an underlying pattern: ˹normal+(speziell normal+)*˼.
Adding delimiters gives ˹"[^\\"]+(\\.[^\\"]+)*"˼. Unfortunately, this misses the table’s last two examples because the new expression requires an ordinary character after the opening quote and after every escaped character. Those examples lack one: strings may contain consecutive escaped characters or an escaped character immediately after the opening quote.
We could replace the pluses with stars: ˹"[^\\"]*(\\.[^\\"]*)*"˼. Does this have the desired effect? More importantly, does it have unwanted side effects?
The desired effects are easy to see: all strings now match, even "\"\"\"". So far so good, but we must check for unintended changes. Does it match anything other than quoted strings? Does it miss valid strings? What about efficiency?
Examine ˹"[^\\"]*(\\.[^\\"]*)*"˼ closely. The initial ˹"[^\\"]*˼ matches once and looks harmless: the required opening quote and any following ordinary characters. No danger. The following ˹(\\.[^\\"]*)*˼ is enclosed by (...)* and can therefore match nothing. Omitting it must leave a correct expression. Doing so gives ˹"[^\\"]*"˼, unquestionably correct for ordinary strings without escaped characters.
Conversely, if ˹(\\.[^\\"]*)*˼ matches once, the actual expression becomes ˹"[^\\"]*\\.[^\\"]*"˼. Even if ˹[^\\"]*˼ matches nothing, giving the actual expression ˹"[^\\"]*\\."˼, no problem arises. Continuing this analysis, which my algebra lessons would call proof by mathematical induction, shows the transformation is correct.
We thus obtain this expression for quoted strings containing escaped quotes:
˹"[^\\"]*(\\.[^\\"]*)*"˼
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