I am looking for a regular expression to split a string on commas. Sounds very simple, but there is another restriction. The parameters on the string could have commas surrounded by parenthesis which should not split the string.
Example:
1, 2, 3, add(4, 5, 6), 7, 8
^ ^ ^ ! ! ^ ^
The string should only be splitted by the commas marked with ^ and not with !.
I found a solution for it here: A regex to match a comma that isn't surrounded by quotes
Regex:
,(?=([^\(]*\([^\)]*\))*[^\)]*$)
But my string could be more complex:
1, 2, 3, add(4, 5, add(6, 7, 8), 9), 10, 11
^ ^ ^ ! ! ! ! ! ^ ^
For this string the result is wrong and i have no clue how to fix this or if it even is possible with regular expressions.
Have anyone an idea how to resolve this problem?
Thanks for your help!