Is there any way to split commas outside quotes, and ignore double single quotes within the quotes? This would be really useful when manipulating with SQL.
I'm trying to split SQL statements, and SQL uses single quote to escape single quote within strings.
e.g.
String source = "ADDRESS.CITY || ', UK''s', ADDRESS.CITY || ', US''s', ADDRESS.CITY || ', UK''s'";
String[] expected = new String[]{
"ADDRESS.CITY || ', UK''s'",
"ADDRESS.CITY || ', US''s'",
"ADDRESS.CITY || ', UK''s'"
};
String[] result = splitElements(source);
assert expected.equals(result);
I have tried Splitting on comma outside quotes and changed it to single quotes i.e.
source.split(",(?=(?:[^\']*\'[^\']*\')*[^\']*$)")
The problem is it doesn't ignore double single quotes.
I have also tried to combine it with Split with single colon but not double colon using regex, but wouldn't be able to get it working so far.