XML technologies / Regular expressions / Practical regex techniques / A few short examples / Nested pairs of parentheses

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

Matching paired elements such as parentheses, angle brackets, square brackets, or braces is a common problem, especially when processing configuration files or programs. Suppose we want to extract a function’s argument list from a C program. Arguments follow the function name and are parenthesized, but may themselves contain parentheses, either in nested function calls or in algebraic expressions. Ignoring nested parentheses at first, we might try: ˹\bfoo\([^)]*\)˼, but this does not work.

In good C tradition, the example function is of course called foo. The highlighted part of the regex is intended to match the function’s arguments. With subject text such as foo(2,●.0) and foo(somevar,●3.7) this works as intended. Unfortunately, the regex also finds foo(bar(somevar),●3.7), which is not what we want. We need something “smarter” than ˹[^)]*˼; possible approaches include:

 

regex

Description

1.

\(.*\)

Literal parentheses with anything between them

2.

\([^)]*\)

From an opening parenthesis to the next closing parenthesis

3.

\([^()]*\)

From an opening parenthesis to the next closing parenthesis, with no parentheses between them

The following figure shows what these expressions would find in sample text.

What our sample expressions would find

Figure: what our sample expressions would find.

As you can see, regex 1 matches too much text. (Note: ˹.*˼ should set off an alarm: are you sure the dot is what you want to quantify with a star? Sometimes it is, but ˹.*˼ is often misused.) Regex 2 matches too little. Regex 3 does not match at all: in isolation, ›(dies)‹ would match, but the opening parenthesis must immediately follow the function name foo. Thus none of the three expressions serves our purpose.

There is a reason for this: Regular expressions cannot match arbitrarily nested parentheses. It simply cannot be done. Until recently, this was universally true. More recently, however, Perl, the .NET languages, and PCRE/PHP offer constructs that do make this possible (see Matching nested pairs with dynamic regular expressions, Matching nested constructs and Parsing text with nested parentheses).

Even without these extensions, a regular expression can be constructed that works up to a certain nesting depth, though not for arbitrary nesting depths. For example, this regex allows just one level of nesting:

˹\([^()]*(\([^()]*\)[^()]*)*\)˼

The thought of allowing deeper nesting already becomes daunting. Yet the following short Perl fragment builds the corresponding regex for a nesting depth $depth. It uses the “string x count” operator to build a string consisting of count concatenated copies of the string.

$regex = '\(' . '(?:[^()]|\(' x $depth . '[^()]*' . '\))*' x $depth . '\)';

The analysis is left to you.

  

 

 

 

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