Mandy
Mandy

Reputation: 31

C# regular expression match

I would like to have a Regex which will match a separating comma phrases of equal amount of opening and closing brackets of the same type between a comma.

for example...

{abc} (def), [ghi], (jkl, mno)
-----------------------------
the match should be:

{abc} (def)  
[ghi]  
(jkl, mno)

I'm working with C# .Net

thanks for advance!

Upvotes: 3

Views: 248

Answers (1)

drf
drf

Reputation: 8699

If there are no nested brackets, you could use:

((?:\{[^}]*\}|\([^)]*\)|\[[^\]]*\])\s*)+

string test  = "{abc} (def), [ghi], (jkl, mno)";
string pattern = @"((?:\{[^}]*\}|\([^)]*\)|\[[^\]]*\])\s*)+";
foreach (Match m in Regex.Matches(test, pattern))
    Console.WriteLine(m.Value);

This prints:

{abc} (def)
[ghi]
(jkl, mno)

Upvotes: 3

Related Questions