Parse CSV with double quote in some cases

You could use Matcher.find with the following regular expression:

\s*("[^"]*"|[^,]*)\s*

Here’s a more complete example:

String s = "a1, a2, a3, \"a4,a5\", a6";
Pattern pattern = Pattern.compile("\\s*(\"[^\"]*\"|[^,]*)\\s*");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
    System.out.println(matcher.group(1));
}

See it working online: ideone

Leave a Comment