XML technologies / Regular expressions / .NET / Advanced regular expressions in .NET / Matching nested constructs

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

Microsoft added an interesting extension to .NET’s regex package, allowing arbitrary nested constructs to be matched, something ordinary regexes cannot do. It is not easy to understand and is described only briefly here.

An example illustrates it most easily:

Dim R As Regex = New Regex(" \(                    " & _
                           "   (?>                 " & _
                           "       [^()]+          " & _
                           "     |                 " & _
                           "       \( (?<TIEFE>)   " & _
                           "     |                 " & _
                           "       \) (?<-TIEFE>)  " & _
                           "   )*                  " & _
                           "   (?(TIEFE)(?!))      " & _
                           " \)                    ",  _
       RegexOptions.IgnorePatternWhitespace)

This regex matches the first complete outer pair of parentheses, including any nested pairs, for example the underlined part of ‘vorher (nixda (oja (hier) isjagut) nachher’. The first opening parenthesis is excluded because it has no closing partner.

Briefly, here is how it works:

  1. Each ‘(’ uses named group ˹(?<TIEFE>)˼ to increment the nesting depth of the regex engine by one, beyond the initial opening parenthesis.

  2. Each ‘)’ uses the new construct ˹(?<-TIEFE>)˼ to subtract one from the corresponding variable.

  3. Using ˹(?(TIEFE)(?!))˼ ensures the variable is zero when the engine reaches the final literal ˹\)˼.

This works because the regex engine’s backtracking stack tracks the number of successfully captured groups. ˹(?<TIEFE>)˼ is simply a named version of ˹()˼, which always matches. Since this group immediately follows ˹\(˼, it can count opening parentheses.

The number of captured ‘TIEFE’ groups, and thus opening parentheses, is stored on the backtracking stack. We want to subtract one from this implicit depth when a closing parenthesis appears. That is precisely what ˹(?<-TIEFE>)˼ does: it removes the last captured ˹(?<TIEFE>)˼ group from the stack. Without such a group, ˹(?<-TIEFE>)˼ fails. This prevents surplus closing parentheses from matching.

The final construct ˹(?(TIEFE)(?!))˼ is an ordinary conditional group matching subexpression ˹(?!)˼ if group ‘TIEFE ’ still exists. It exists only if too few closing parentheses were found and ˹(?<-TIEFE>)˼ did not reduce every depth level to zero. The parentheses are then unbalanced, so empty negative lookahead ˹(?!)˼ forces failure.

Whew! That is how .NET regexes can match nested constructs.

  

 

 

 

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