Is it possible to split a String with the conditions as below?
- split by , (i.e. comma)
- on each element, ignore checking comma within the first ' and the last '
- on each element, ignore checking comma within the first ( and the last )
e.g.
String source = "to_char(DATE, 'YYYY,MM,DD'), 'I am sad :(', to_char(DATE, ('YYYY(MM,DD)')), to_char(DATE, ('YYYY,MM,DD)')), to_char(DATE, ('YYYY(MM,DD')), NAME, to_char(DATE, '(YYYY)MM,DD'), CITY || ', (UK)', CITY || ', US''s CITY', CITY || ', UK'";
String[] expected = new String[]{
"to_char(DATE, 'YYYY,MM,DD')",
"'I am sad :('",
"to_char(DATE, ('YYYY(MM,DD)'))", // brackets within quotes within brackets
"to_char(DATE, ('YYYY,MM,DD)'))", // missing open bracket in quotes
"to_char(DATE, ('YYYY(MM,DD'))", // missing close bracket in quotes
"NAME",
"to_char(DATE, '(YYYY)MM,DD')",
"CITY || ', (UK)'",
"CITY || ', US''s CITY'", // escape a single quote in quotes
"CITY || ', UK'"
};
String[] result = splitElements(source);
assert expected.equals(result);
The first 2 bullet points can be achieved by Splitting on comma outside quotes when escaped quotes exist
This would be really useful when manipulating with SQL. E.g. split the items, append, insert, prepend items etc.
Thanks in advance.