I need to split a string by commas, but I have a problem with this case:
TEXT EXAMPLE (THIS IS (A EXAMPLE, BUT NOT WORKS, FOR ME)), SECOND , THIRD
I would like to split and get:
var[0] = "TEXT EXAMPLE (THIS IS (A EXAMPLE, BUT NOT WORKS, FOR ME))"
var[1] = "SECOND"
var[2] = "THIRD"
Thank you
Here's a very simple parser approach that works for your example:
You can use this negative lookahead based regex:
This regex is finding a comma with an assertion that makes sure comma is not in parentheses. This is done using a negative lookahead that first consumes all matching
(
and)
and then a)
. This assumes parentheses are balanced and unescaped.RegEx Demo
Code:
Or:
Thanks to jonrsharpe :
Result:
You can just use
rsplit
: