1
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)
nam
  • 21,967
  • 37
  • 158
  • 332
  • 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. – Federico Piazza Jun 28 '15 at 02:24

2 Answers2

1

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.

Community
  • 1
  • 1
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • I agree that one can use a balance algorithm in a "for" loop to achieve the same. In my case a RegEx with .NET Regular Expressions Balancing Groups also works. – nam Jun 28 '15 at 04:51
0

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)
l'L'l
  • 44,951
  • 10
  • 95
  • 146