0

I am trying to figure out a way to split values based on a comma in regex but the problem is that I want to ignore commas that are inside the parenthesis ()

example :

'hist1234,geog293,lap_933,(a-20,110,60),(z-8,9,10),POLY5,9,3,8,ro water'

expected output :

['hist1234', 'geog293', 'lap_933', '(a-20,110,60)', '(z-8,9,10)', 'POLY5,9,3,8', 'ro water']

I tried: ,\s*(?![^()]*)) for the comma and ignoring the comma in brackets but im unsure about the ignoring if its a number followed by a comma

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Sid
  • 23
  • 6
  • Do you know this? https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags IMHO your case is similar. – Thomas Weller Jul 12 '21 at 14:16
  • I need to use Regex not to parse through HTML but for another application – Sid Jul 12 '21 at 14:18
  • The regex code also works, I just need help with the number followed by comma part – Sid Jul 12 '21 at 14:18
  • 2
    There's hardly a difference between round braces and angle brackets... Is it guaranteed that there's no parenthesis inside a parenthesis? – Thomas Weller Jul 12 '21 at 14:19
  • no parenthesis inside a parenthesis in this one yep – Sid Jul 12 '21 at 14:20

1 Answers1

2

Since you guaranteed "no parenthesis inside a parenthesis" then this would work:

regex

,(?![^()]+\))(?=[a-zA-Z(])

replace

, 

^ comma + space

If splitting then just ignore the replace.

https://regex101.com/r/Mk8mAI/1

  • , - capture a comma
  • (?![^()]+\)) - guarantee that ahead of me is not content followed by a closing parenthesis
  • (?=[a-zA-Z(]) - guarantee that ahead of me is a letter or opening parenthesis
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77