-1

I have a string that looks like:

AA,BB,CC(DD,EE(FF))

that I want to split at the root level, leaving the nested list intact. So the above string would be split into:

AA, BB, and CC(DD,EE(FF))

I know how to do this without a regular expression, but I'm having a hard time writing a regex that can identify the outer most commas and ignores the commas in the paratheses.

Avinash Raj
  • 166,785
  • 24
  • 204
  • 249
Tyzone34
  • 107
  • 1
  • 6
  • 2
    Obviously post should be getting downvotes due to amount of effort demonstrated in it... Maybe you should show what you've tried and explain why searching for something like https://www.bing.com/search?q=regex+matching+braces (i.e. https://stackoverflow.com/questions/14952113/how-can-i-match-nested-brackets-using-regex) did not help... Than at least post has a chance... – Alexei Levenkov Jan 23 '18 at 04:26

1 Answers1

0

In .Net you can use balancing groups to capture nested structures. This is probably more complicated than the code you already have though:

(?>
    (?<Open>\()     # Increase stack depth when matching (
    |
    (?<-Open>\))    # Decrease stack depth when matching )
    |
    [^(),]          # Match any other character
    |
    (?(Open),|(?!)) # Allow commas only while inside parentheses.
)+
(?(Open)(?!))  # Ensure we have no extra open parentheses.

Live example on Regex Storm

A simpler (yet uglier) approach that works in all regex flavors is to limit the levels of nesting you support. For example:

Basically we're adding another (?:[^()]|\([^()]*\))* for each new level we want to match.

Kobi
  • 130,553
  • 41
  • 252
  • 283