2

I'm having kind of a nightmare about this regEx I need to provide. The idea is basically to find all ',' characters between parenthesis within a certain string... but if inside that parenthesis there's another parenthesis that contain ',' then don't include this.

Sample:

(hello, something, hi, (another, one), last)

So the idea would be to detect and match 5 groups

1. (hello,
2. something,
3. hi,
4. (another, one),
5. last)

I've been struggling but I can't break it up like this, instead I get 6 groups

1. (hello,
2. something,
3. hi,
4. (another,  <-- not ok
5. one), <-- not ok
6. last)

Have been trying with some regEx but I can't detect the double ( ) pattern, the best I could achieved so far was

(?!\(.*)[,](?=.*\))

Thanks for the time and help

11
  • 1
    This can't be done with a regex. Commented Oct 9, 2018 at 12:01
  • 2
    Try ([A-Za-z]+|\([^()]+,[^()]+\)) Commented Oct 9, 2018 at 12:08
  • 1
    How deep can the inner parentheses be nested Commented Oct 9, 2018 at 12:18
  • 1
    @ran_0315 actually this detects the inner parenthesis with the comma, which I good I suppose with some extra coding. Will take it into account if this can't be solved with a single regEx. thanks for the tip Commented Oct 9, 2018 at 12:27
  • 2
    Try \([^()]*\),?|\S[^,]*,?. See live demo here regex101.com/r/6jg92v/1 Commented Oct 9, 2018 at 12:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.