Search code examples
c#regexbalancing-groups

Regular Expressions Balancing Group


I am trying to match balancing braces ({}) in strings. For example, I want to balance the following:

if (a == 2)
{
  doSomething();
  { 
     int x = 10;
  }
}

// this is a comment

while (a <= b){
  print(a++);
} 

I came up with this regex from MSDN, but it doesn't work well. I want to extract multiple nested matching sets of {}. I am only interested in the parent match

   "[^{}]*" +
   "(" + 
   "((?'Open'{)[^{}]*)+" +
   "((?'Close-Open'})[^{}]*)+" +
   ")*" +
   "(?(Open)(?!))";

Solution

  • You're pretty close.

    Adapted from the second answer on this question (I use that as my canonical "balancing xxx in C#/.NET regex engine" answer, upvote it if it helped you! It has helped me in the past):

    var r = new Regex(@"
    [^{}]*                  # any non brace stuff.
    \{(                     # First '{' + capturing bracket
        (?:                 
        [^{}]               # Match all non-braces
        |
        (?<open> \{ )       # Match '{', and capture into 'open'
        |
        (?<-open> \} )      # Match '}', and delete the 'open' capture
        )+                  # Change to * if you want to allow {}
        (?(open)(?!))       # Fails if 'open' stack isn't empty!
    )\}                     # Last '}' + close capturing bracket
    "; RegexOptions.IgnoreWhitespace);