title: Regular Expressions Balancing Group
body: <p>I am trying to match balancing braces ({}) in strings. For example, I want to balance the following:</p>
  
  <pre><code>if (a == 2)
  {
    doSomething();
    { 
       int x = 10;
    }
  }
  
  // this is a comment
  
  while (a &lt;= b){
    print(a++);
  } 
  </code></pre>
  
  <p>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</p>
  
  <pre><code>   "[^{}]*" +
     "(" + 
     "((?'Open'{)[^{}]*)+" +
     "((?'Close-Open'})[^{}]*)+" +
     ")*" +
     "(?(Open)(?!))";
  </code></pre>