0

I'm trying to use a regular expression in C# csv that can contain: code as

a,a,(a,b,(a)).

I have tried the following:

var result = (from Match m in Regex.Matches(input, @"([^,()]+(\([^()]*\))?") select m.Value).ToArray();
string result1 = ConvertStringArrayToString(result);
textBox2.Text = result1;

but i get output like this:

a
a
a
b(a)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Bala .K
  • 1
  • 2

1 Answers1

0

You need balancing group definitions to make it work:

[^,()]*(?:\(([^()]*|(?<open>\()|(?<-open>\)))*(?(open)(?!))\))*[^,()]*

DEMO

A good introduction into BGD is here and here

Community
  • 1
  • 1
Sebastian Schumann
  • 3,204
  • 19
  • 37