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)(?!))";