Niklas Raab
Niklas Raab

Reputation: 31

How to Split String at "," with Regex

String:

"ab, ac, Convert(ab,ac), test"

I want this stringArray:

ab
ac
Convert(ab,ac)
test

Upvotes: 1

Views: 68

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

Just split your input acccording to ,\s+ or , + regexes. \s+ matches one or more space characters.

string value = "ab, ac, Convert(ab,ac), test";
string[] lines = Regex.Split(value, @", +");
foreach (string line in lines) {
Console.WriteLine(line);
}

Output:

ab
ac
Convert(ab,ac)
test

IDEONE

Upvotes: 0

vks
vks

Reputation: 67968

,\s*(?![^(]*\))

Try this.Replace by \n.See demo.

https://regex101.com/r/nL5yL3/28

This will work on inputs like ab, ac, Convert(ab,ac),test,bc,mc, too

Upvotes: 1

Related Questions