我正在尝试匹配字符串中的平衡大括号 ({})。例如,我想平衡以下内容:
if (a == 2)
{
doSomething();
{
int x = 10;
}
}
// this is a comment
while (a <= b){
print(a++);
}
我从 MSDN 想出了这个正则表达式,但效果不佳。我想提取 {} 的多个嵌套匹配集。我只对父匹配感兴趣
"[^{}]*" +
"(" +
"((?'Open'{)[^{}]*)+" +
"((?'Close-Open'})[^{}]*)+" +
")*" +
"(?(Open)(?!))";