简体   繁体   中英

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:

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).

Imho, you should use a simple balance algorithm in a for loop. 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:

Console.WriteLine(c.Value)

Result:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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