简体   繁体   English

正则表达式匹配嵌套的括号,包括开始和结束括号

[英]RegEx to match nested parentheses including the start and end parentheses

string st = "this (a,b) and this (s,(r,t),u) is a test";
var regex = new Regex(@"\(([^()]+| (?<Level>\()| (?<-Level>\)))+(?(Level)(?!))\)", RegexOptions.IgnorePatternWhitespace);

foreach (Match c in regex.Matches(input))
{
  Console.WriteLine(c.Value.Trim('(', ')'));
}

The above C# code in .NET 4.5 correctly returns: NET 4.5中的上述C#代码正确返回:

a,b
s,(r,t),u

But I need the output including the parentheses as: 但是我需要包含括号的输出为:

(a,b)
(s,(r,t),u)

You can't do this with regex. 您不能使用正则表达式执行此操作。

You can use regex in a greedy or lazy way, but you can't apply logic to handle balancing of parentheses. 您可以以贪婪或懒惰的方式使用正则表达式,但不能应用逻辑来处理括号的平衡。

If you use \\(.*\\) you will capture everything (greedy) from the first to the last parentheses and if you use \\(.*?\\) (lazy or ungreedy) you will match from the first to the second one. 如果使用\\(.*\\) ,则将捕获从第一个到最后一个括号的所有内容(贪婪);如果您使用\\(.*?\\) (惰性或不贪婪的),则将匹配第一个到第二个。 Regex is no the right tool to match embedded strings (that's why they are also a bad idea to match embedded xhtml tags). 正则表达式不是匹配嵌入式字符串的正确工具(这就是为什么匹配嵌入式xhtml标记也不是一个好主意的原因)。

Imho, you should use a simple balance algorithm in a for loop. 恕我直言,您应该在for循环中使用简单的余额算法。 However, if you still want to use regex you can check this thread . 但是,如果您仍然想使用正则表达式,则可以检查此线程

If I understand correctly you currently have the output of: 如果我理解正确,那么您当前的输出为:

a,b
s,(r,t),u

Since you are using Trim('(', ')') it removes the outer parentheses — to include them use: 由于您正在使用Trim('(', ')')因此会删除外部括号-要使用它们,请使用以下括号:

Console.WriteLine(c.Value)

Result: 结果:

(a,b)
(s,(r,t),u)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM