If all of your quote are matched, every comma you want to split at will be followed by an even number of "
. So you could use a lookahead and stuff this thing into myString.split(pattern, -1)
:
,(?=(?:(?:[^\"]*\"){2})*[^\"]*$)
This will only match if there is an even number of "
between the comma in question and the end of the string.
Note that the -1
argument for split
is important, otherwise trailing empty strings will be omitted.
Side note: I don't know how well the Java regex engine optimizes, so this lookahead might be quite inefficient if it fails, because it unnecessarily backtracks. If you experience performance issues, try making the quantifiers possessive:
,(?=(?:(?:[^\"]*+\"){2})*+[^\"]*+$)
This will stop the engine from backtracking.