XML technologies / Regular expressions / The art of writing regular expressions / Unrolling the loop / Examples of unrolling loops

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

We now understand the basic recipe for unrolling a loop and will apply it to examples from earlier chapters.

 

Multicharacter “quotes”

The following HTML-tag example comes from How regex engines work (see Lazy quantifiers):

<B>                # Passt auf das <B> am Anfang ...
(                  #     So viele vom Folgenden wie nötig ...
  (?!  </?B>  )    #        Wenn wir weder <B> noch </B> haben ...
  .                #             ... ist jedes Zeichen recht.
)*                 #
</B>               # ... bis das schließende Begrenzungszeichen passen kann.

Using ˹[^<]˼ as the normal part and ˹(?!</?B>)<˼ as the special part gives this unrolled version:

<B>                # Passt auf das <B> am Anfang ...
  (?> [^<]* )      # »Normal« finden ...
  (?>              # So viele vom Folgenden wie nötig ...
     (?! </?B> )   #     Wenn wir weder <B> noch </B> haben ...
     <             #     Einmal »speziell« ...
     [^<]*         #     ... und eine beliebige Anzahl von »normal«
   )*              #
</B>               # ... bis zum schließenden Begrenzungszeichen.

Atomic groups are unnecessary here, but make the regex faster on a partial match.

 

Unrolling continuation-line matching

The continuation-line example in Practical regex techniques (see Continuation lines) led to expression ˹^\w+=([^\n\\]|\\.)*˼, a good candidate for our recipe:

^ \w+ =                 # Schlüssel und Gleichheitszeichen.
# Nun wird der Wert erkannt und abgespeichert ...
(
   (?> [^\n\\]* )       # »normal«*
   (?> \\. [^\n\\]* )*  # ( »speziell« »normal«* )*
)

Again, atomic groups do not affect what the regex matches, but help the engine detect failure faster.

 

Unrolling CSV matching

In Practical regex techniques, a longer example processed CSV data and produced this result in Disabling the transmission:

(?:^|,)
(?:                             # Entweder ein Feld in Anführungszeichen ...
      "                         #       Öffnendes Anführungszeichen
        ( (?: [^"] | "" )* )    #         Anführungszeichen nur verdoppelt erlaubt
      "                         #       Schließendes Anführungszeichen
   |                            # ... oder ...
      ( [^",]+ )                # ... ein ›nacktes‹ Feld ohne Kommas und Anführungszeichen.
)

We then used anchor ˹\G˼ to prevent the transmission from starting the regex at unsuitable positions. Now we know how to unroll this kind of loop; let us see whether the technique applies here too.

The part matching a Microsoft-style string field, ˹(?:[^"]|"")*˼, looks promising. The regex is already arranged so that its normal and special parts are almost obvious: ˹[^"]˼ and ˹""˼.

Here are those parts in the original Perl program:

while ($zeile =~ m{
          \G(?:^|,)
          (?:               # Entweder ein Feld in Anführungszeichen (mit "" für jedes ") ...
             "              # Öffnendes Anführungszeichen
              ( (?> [^"]* ) (?> "" [^"]* )*  )
             "              # Schließendes Anführungszeichen
           |                # ... oder ...
             ( [^",]* )     # ... ein ›nacktes‹ Feld ohne Kommas und Anführungszeichen.
          )
      }gx)
{
   if (defined $2) {
       $feld = $2;
   } else {
       $feld = $1;
       $feld =~ s/""/"/g;
   }
   print "[$feld]";         # Feld für Debugging-Zwecke ausgeben.
     .                      # Wert in $feld kann jetzt weiterverarbeitet werden.
     . 
     .                       
}

Again, atomic groups do not change the program’s behavior, but make the regex more efficient.

  

 

 

 

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