@Test public void parseCSVLine2() { String line = "\"This is a test\" \"Another test\" \"No spaces\" \"Between these two\""; List<Object> tokens = CsvParser.parseCSVLine(line, " "); assertThat(tokens, equalTo(Arrays.<Object>asList("This is a test", "Another test", "No spaces", "Between these two"))); }
public static List<Object> parseCSVLine(String line, String separatorChar) { String regex = "\\G(?:^|" + separatorChar + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + separatorChar + "]*)" + ")"; Matcher mMain = Pattern.compile(regex).matcher(""); Matcher mQuote = Pattern.compile("\"\"").matcher(""); Matcher mDouble = Pattern.compile(DOUBLE_REGEX_WITH_NAN).matcher(""); List<Object> values = new ArrayList<>(); mMain.reset(line); while (mMain.find()) { Object value; if (mMain.start(2) >= 0) { String field = mMain.group(2); if (mDouble.reset(field).matches()) { value = Double.parseDouble(field); } else { value = field; } } else { value = mQuote.reset(mMain.group(1)).replaceAll("\""); } values.add(value); } return values; }
CsvParser { public static List<Object> parseCSVLine(String line, String separatorChar) { String regex = "\\G(?:^|" + separatorChar + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + separatorChar + "]*)" + ")"; Matcher mMain = Pattern.compile(regex).matcher(""); Matcher mQuote = Pattern.compile("\"\"").matcher(""); Matcher mDouble = Pattern.compile(DOUBLE_REGEX_WITH_NAN).matcher(""); List<Object> values = new ArrayList<>(); mMain.reset(line); while (mMain.find()) { Object value; if (mMain.start(2) >= 0) { String field = mMain.group(2); if (mDouble.reset(field).matches()) { value = Double.parseDouble(field); } else { value = field; } } else { value = mQuote.reset(mMain.group(1)).replaceAll("\""); } values.add(value); } return values; } }
CsvParser { public static List<Object> parseCSVLine(String line, String separatorChar) { String regex = "\\G(?:^|" + separatorChar + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + separatorChar + "]*)" + ")"; Matcher mMain = Pattern.compile(regex).matcher(""); Matcher mQuote = Pattern.compile("\"\"").matcher(""); Matcher mDouble = Pattern.compile(DOUBLE_REGEX_WITH_NAN).matcher(""); List<Object> values = new ArrayList<>(); mMain.reset(line); while (mMain.find()) { Object value; if (mMain.start(2) >= 0) { String field = mMain.group(2); if (mDouble.reset(field).matches()) { value = Double.parseDouble(field); } else { value = field; } } else { value = mQuote.reset(mMain.group(1)).replaceAll("\""); } values.add(value); } return values; } private  CsvParser(String separators, Header header); }
CsvParser { public static List<Object> parseCSVLine(String line, String separatorChar) { String regex = "\\G(?:^|" + separatorChar + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + separatorChar + "]*)" + ")"; Matcher mMain = Pattern.compile(regex).matcher(""); Matcher mQuote = Pattern.compile("\"\"").matcher(""); Matcher mDouble = Pattern.compile(DOUBLE_REGEX_WITH_NAN).matcher(""); List<Object> values = new ArrayList<>(); mMain.reset(line); while (mMain.find()) { Object value; if (mMain.start(2) >= 0) { String field = mMain.group(2); if (mDouble.reset(field).matches()) { value = Double.parseDouble(field); } else { value = field; } } else { value = mQuote.reset(mMain.group(1)).replaceAll("\""); } values.add(value); } return values; } private  CsvParser(String separators, Header header); String getSeparators(); CsvParser withSeparators(String separators); Header getHeader(); CsvParser withHeader(Header header); CsvParserResult parse(Reader reader); static List<Object> parseCSVLine(String line, String separatorChar); }
CsvParser { public static List<Object> parseCSVLine(String line, String separatorChar) { String regex = "\\G(?:^|" + separatorChar + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + separatorChar + "]*)" + ")"; Matcher mMain = Pattern.compile(regex).matcher(""); Matcher mQuote = Pattern.compile("\"\"").matcher(""); Matcher mDouble = Pattern.compile(DOUBLE_REGEX_WITH_NAN).matcher(""); List<Object> values = new ArrayList<>(); mMain.reset(line); while (mMain.find()) { Object value; if (mMain.start(2) >= 0) { String field = mMain.group(2); if (mDouble.reset(field).matches()) { value = Double.parseDouble(field); } else { value = field; } } else { value = mQuote.reset(mMain.group(1)).replaceAll("\""); } values.add(value); } return values; } private  CsvParser(String separators, Header header); String getSeparators(); CsvParser withSeparators(String separators); Header getHeader(); CsvParser withHeader(Header header); CsvParserResult parse(Reader reader); static List<Object> parseCSVLine(String line, String separatorChar); static final CsvParser AUTOMATIC; }