import java.util.regex.*; String regex = // Puts a doublequoted field into group(1), an unquoted field into group(2) "\\G(?:^|,) \n"+ "(?: \n"+ " # Either a double-quoted field . . . \n"+ " \" # field's opening quote \n"+ " ( (?: [^\"]++ | \"\" )*+ ) \n"+ " \" # field's closing quote \n"+ " |# . . . or . . . \n"+ " # some non-quote/non-comma text . . . \n"+ " ( [^\",]* ) \n"+ " ) \n"; // Create a matcher, using the regex above, with dummy text for the time being. Matcher mMain = Pattern.compile(regex, Pattern.COMMENTS).matcher(""); // Create a matcher for 「"" , with dummy text for the time being Matcher mQuote = Pattern.compile("\"\"").matcher(""); // Above is the preparation; the code below is executed on a per-line basis mMain.reset(line); // Use this line of CSV text in the processing below while (mMain.find()) { String field; if (mMain.start(2) >= 0) field = mMain.group(2); // The field is unquoted, so we can use it as is else // The field is quoted, so we must replace paired doublequotes with one double quote field = mQuote.reset(mMain.group(1)).replaceAll("\""); // We can now work with field . . . System.out.println("Field [" + field + "]"); } ----------------------------------------------------------------------------- Copyright 1997-2024 Jeffrey Friedl