简体   繁体   中英

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!

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)

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