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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Rodrigo Martinez
  • 913
  • 3
  • 13
  • 29
  • 1
    This can't be done with a regex. – melpomene Oct 09 '18 at 12:01
  • Maybe you can remove the outer parentheses and then use a solution like in https://stackoverflow.com/questions/41071509. However, this sounds as if you need to write some parsing function here. – Wiktor Stribiżew Oct 09 '18 at 12:04
  • 2
    Try `([A-Za-z]+|\([^()]+,[^()]+\))` – Nambi_0915 Oct 09 '18 at 12:08
  • 1
    How deep can the inner parentheses be nested – JGNI Oct 09 '18 at 12:18
  • @JGNI only once.... – Rodrigo Martinez Oct 09 '18 at 12:27
  • 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 – Rodrigo Martinez Oct 09 '18 at 12:27
  • 2
    Try `\([^()]*\),?|\S[^,]*,?`. See live demo here https://regex101.com/r/6jg92v/1 – revo Oct 09 '18 at 12:48
  • What I commented above doesn't produce desired output on a subject string that doesn't have a nested pair of parentheses. Please try `(?!^\s*\()\([^()]*\),?|\S[^,\r\n]+,?` instead. See it in action https://regex101.com/r/7mjSKH/1 – revo Oct 09 '18 at 13:12
  • There's a bunch of trivial ways to break that example including just changing the word "something" to "a". Agree with the melpomene comment though this doesn't seem like a smart job for regex, you can spend a bunch of time writing quite a complicated regex that will only work under narrow conditions and not in a generalized sense. – Phil B Oct 09 '18 at 19:55
  • this is not possible just with regex. you may want to read a response I've just posted about this > https://stackoverflow.com/questions/42124287/handling-duplicate-regex-group-name-in-java-c-translation/52800845#52800845 – Pedro Rodrigues Oct 14 '18 at 09:00
  • it boils down to 'regex engine does not keep state. They just shove stuff into buckets.' – Pedro Rodrigues Oct 14 '18 at 09:02

0 Answers0