Geeks With Blogs
Eron Wright - All Killer No Filler blog

It is a known hard problem to match nested parenthesis pairs using regular expressions.  Put another way, regular expressions to not typically support counting occurrences.  .NET has a little-known RegEx construct for doing just that called the “balancing group definition“:

Balancing group definition. Deletes the definition of the previously defined group name2 and stores in group name1 the interval between the previously defined name2 group and the current group. If no group name2 is defined, the match backtracks. Because deleting the last definition of name2 reveals the previous definition of name2, this construct allows the stack of captures for group name2 to be used as a counter for keeping track of nested constructs such as parentheses. In this construct, name1 is optional. You can use single quotes instead of angle brackets; for example, (?'name1-name2').

I found great information on using the balancing group definition here:

http://www.oreilly.com/catalog/regex2/chapter/index.html

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpcongroupingconstructs.asp?frame=true

In my opinion this construct greatly enhances the utility of regular expressions.

 

Posted on Monday, March 29, 2004 12:01 PM Technology | Back to top


Comments on this post: Matching Parenthesis Pairs Using Regular Expressions

# re: Matching Parenthesis Pairs Using Regular Expressions
Requesting Gravatar...
The short description from MSDN doesn't really explain this to me -- can you give a better example?
Left by Doug Moore on Jun 25, 2004 4:58 PM

# re: Matching Parenthesis Pairs Using Regular Expressions
Requesting Gravatar...
Do you have a specific example. I don't have the book and the sample chapter does not specificially deal with "balancing group definition" I would like to match XML tags which is similar to the problem with parenthesis with the exception that XML tags of course are more than one character and the start tag can have attributes.

Kevin
Left by Kevin Burton on Aug 27, 2004 5:22 PM

# re: Matching Parenthesis Pairs Using Regular Expressions
Requesting Gravatar...
See chapter 9, pg 430 in the O'Reilly book (online, see the link). I think that the example can be applied to XML matching.
Left by Eron Wright on Aug 28, 2004 3:28 PM

# re: Matching Parenthesis Pairs Using Regular Expressions
Requesting Gravatar...
For a dicussion and a few example take a look at my post (http://weblogs.asp.net/whaggard/archive/2005/02/20/377025.aspx)
Left by Wes Haggard on Feb 24, 2005 12:36 PM

# re: Matching Parenthesis Pairs Using Regular Expressions
Requesting Gravatar...
How can I get rid of a comma and a paranthesis inside a tag.
eg.

<p(,)> this is the function, calculate() </p(,)>


after replace it should look like:

<p> this is the function, calculate() </p>
Left by Don on Apr 22, 2005 3:19 PM

# re: Matching Parenthesis Pairs Using Regular Expressions
Requesting Gravatar...
Based on all the links from this page the following code should return a match, but it doesn't. I am using the 1.1 framework. What am I doing wrong?

Dim m As Match = Regex.Match( _
"before (nope (yes (here) okay) after", _
"\( " & _
" (?> " & _
" [ˆ()]+ " & _
" | " & _
" \( (?<DEPTH>) " & _
" | " & _
" \) (?<-DEPTH>) " & _
" )+ " & _
" (?(DEPTH)(?!)) " & _
"\)", _
RegexOptions.IgnorePatternWhitespace)
Left by Wyatt Haase on Oct 21, 2005 2:05 PM

# Examples of using balancing groups
Requesting Gravatar...
This post covers some interesting uses for balancing groups: Fun With .NET's Regex Balancing Groups
Left by Steve on Jan 25, 2008 5:16 PM

# re: Matching Parenthesis Pairs Using Regular Expressions
Requesting Gravatar...
.Net has supplied its RegEx system with a push-down (capture) stack (with implicit push and pop operations: (?<group>xxx) pushed while (?<-group>xxx) pops). The construct is no longer technically a Regular Expression (Finite Automaton) but something more like a PDA (=Push-Down Automaton) which has infinite memory and can recognize a lot more complex patterns (Context-Free Grammars=CFGs)
Left by Hassan on Mar 08, 2010 3:42 AM

# re: Matching Parenthesis Pairs Using Regular Expressions
Requesting Gravatar...
regular expression for matching parenthesis
like )(
Left by sampat on Mar 16, 2012 6:40 AM

Your comment:
 (will show your gravatar)


Copyright © Eron Wright | Powered by: GeeksWithBlogs.net | Join free