0

I read the question and answer and this is not a duplicate. I would like to find all comma and split the string by them which are outside of all parenthesis. Consider the following string

abc(def(100, test(2, 3, 5)), tttt), 1111

It would be splitted then to 2 substrings:

abc(def(100, test(2, 3, 5)), tttt)
1111

The only , which is outside of any parenthesis in the string is , before 1111.

Is there a way to so with regex?

Some Name
  • 6,872
  • 4
  • 9
  • 32
  • I think in general you might need a parser here, especially if the depth of the nested parentheses is not known. – Tim Biegeleisen Feb 28 '18 at 01:33
  • 4
    To quote the not-a-duplicate answer: *Assuming that there can be no nested parens (otherwise, you can't use a Java Regex for this task because recursive matching is not supported)* – shmosel Feb 28 '18 at 01:33
  • 1
    You can't do it with regex, because internally, a regex is build around an automaton with finitely many states. Here, you would have to keep track of arbitrarily deeply nested parentheses like `((((((((((((((((((((((((((((((this)))))))))))))))))))))))))))))),()` – Andrey Tyukin Feb 28 '18 at 02:01
  • @AndreyTyukin Actually, it should be possible to do it with a regex -- as shown [here](https://stackoverflow.com/questions/47162098/is-it-possible-to-match-nested-brackets-with-regex-without-using-recursion-or-ba). Writing the required regex left as an exercise to the student. – AJNeufeld Feb 28 '18 at 02:13
  • 1
    I have already answered the question, see [**this demo**](https://ideone.com/93yjvJ) from [this question](https://stackoverflow.com/a/37978758/3832970). – Wiktor Stribiżew Feb 28 '18 at 06:04

0 Answers0