@Test public void parseCSVLine2() { String line = "\"This is a test\" \"Another test\" \"No spaces\"\"Between these two\""; List<Object> tokens = StringUtil.parseCSVLine(line, "\\s*"); 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 separatorRegex) { List<Object> matches = new ArrayList<>(); int currentPosition = 0; Matcher separatorMatcher = Pattern.compile("^" + separatorRegex).matcher(line); Matcher stringMatcher = Pattern.compile("^" + QUOTED_STRING_REGEX).matcher(line); Matcher doubleMatcher = Pattern.compile("^" + DOUBLE_REGEX).matcher(line); while (currentPosition < line.length()) { if (stringMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { String token = line.substring(currentPosition + 1, stringMatcher.end() - 1); matches.add(unescapeString(token)); currentPosition = stringMatcher.end(); } else if (doubleMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { Double token = Double.parseDouble(line.substring(currentPosition, doubleMatcher.end())); matches.add(token); currentPosition = doubleMatcher.end(); } else { throw new IllegalArgumentException("Can't parse line: expected token at " + currentPosition + " (" + line + ")"); } if (currentPosition < line.length()) { if (!separatorMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { throw new IllegalArgumentException("Can't parse line: expected separator at " + currentPosition + " (" + line + ")"); } currentPosition = separatorMatcher.end(); } } return matches; }
StringUtil { public static List<Object> parseCSVLine(String line, String separatorRegex) { List<Object> matches = new ArrayList<>(); int currentPosition = 0; Matcher separatorMatcher = Pattern.compile("^" + separatorRegex).matcher(line); Matcher stringMatcher = Pattern.compile("^" + QUOTED_STRING_REGEX).matcher(line); Matcher doubleMatcher = Pattern.compile("^" + DOUBLE_REGEX).matcher(line); while (currentPosition < line.length()) { if (stringMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { String token = line.substring(currentPosition + 1, stringMatcher.end() - 1); matches.add(unescapeString(token)); currentPosition = stringMatcher.end(); } else if (doubleMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { Double token = Double.parseDouble(line.substring(currentPosition, doubleMatcher.end())); matches.add(token); currentPosition = doubleMatcher.end(); } else { throw new IllegalArgumentException("Can't parse line: expected token at " + currentPosition + " (" + line + ")"); } if (currentPosition < line.length()) { if (!separatorMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { throw new IllegalArgumentException("Can't parse line: expected separator at " + currentPosition + " (" + line + ")"); } currentPosition = separatorMatcher.end(); } } return matches; } }
StringUtil { public static List<Object> parseCSVLine(String line, String separatorRegex) { List<Object> matches = new ArrayList<>(); int currentPosition = 0; Matcher separatorMatcher = Pattern.compile("^" + separatorRegex).matcher(line); Matcher stringMatcher = Pattern.compile("^" + QUOTED_STRING_REGEX).matcher(line); Matcher doubleMatcher = Pattern.compile("^" + DOUBLE_REGEX).matcher(line); while (currentPosition < line.length()) { if (stringMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { String token = line.substring(currentPosition + 1, stringMatcher.end() - 1); matches.add(unescapeString(token)); currentPosition = stringMatcher.end(); } else if (doubleMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { Double token = Double.parseDouble(line.substring(currentPosition, doubleMatcher.end())); matches.add(token); currentPosition = doubleMatcher.end(); } else { throw new IllegalArgumentException("Can't parse line: expected token at " + currentPosition + " (" + line + ")"); } if (currentPosition < line.length()) { if (!separatorMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { throw new IllegalArgumentException("Can't parse line: expected separator at " + currentPosition + " (" + line + ")"); } currentPosition = separatorMatcher.end(); } } return matches; } private  StringUtil(); }
StringUtil { public static List<Object> parseCSVLine(String line, String separatorRegex) { List<Object> matches = new ArrayList<>(); int currentPosition = 0; Matcher separatorMatcher = Pattern.compile("^" + separatorRegex).matcher(line); Matcher stringMatcher = Pattern.compile("^" + QUOTED_STRING_REGEX).matcher(line); Matcher doubleMatcher = Pattern.compile("^" + DOUBLE_REGEX).matcher(line); while (currentPosition < line.length()) { if (stringMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { String token = line.substring(currentPosition + 1, stringMatcher.end() - 1); matches.add(unescapeString(token)); currentPosition = stringMatcher.end(); } else if (doubleMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { Double token = Double.parseDouble(line.substring(currentPosition, doubleMatcher.end())); matches.add(token); currentPosition = doubleMatcher.end(); } else { throw new IllegalArgumentException("Can't parse line: expected token at " + currentPosition + " (" + line + ")"); } if (currentPosition < line.length()) { if (!separatorMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { throw new IllegalArgumentException("Can't parse line: expected separator at " + currentPosition + " (" + line + ")"); } currentPosition = separatorMatcher.end(); } } return matches; } private  StringUtil(); static String unquote(String quotedString); static String unescapeString(String escapedString); static List<Object> parseCSVLine(String line, String separatorRegex); }
StringUtil { public static List<Object> parseCSVLine(String line, String separatorRegex) { List<Object> matches = new ArrayList<>(); int currentPosition = 0; Matcher separatorMatcher = Pattern.compile("^" + separatorRegex).matcher(line); Matcher stringMatcher = Pattern.compile("^" + QUOTED_STRING_REGEX).matcher(line); Matcher doubleMatcher = Pattern.compile("^" + DOUBLE_REGEX).matcher(line); while (currentPosition < line.length()) { if (stringMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { String token = line.substring(currentPosition + 1, stringMatcher.end() - 1); matches.add(unescapeString(token)); currentPosition = stringMatcher.end(); } else if (doubleMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { Double token = Double.parseDouble(line.substring(currentPosition, doubleMatcher.end())); matches.add(token); currentPosition = doubleMatcher.end(); } else { throw new IllegalArgumentException("Can't parse line: expected token at " + currentPosition + " (" + line + ")"); } if (currentPosition < line.length()) { if (!separatorMatcher.region(currentPosition, line.length()).useAnchoringBounds(true).find()) { throw new IllegalArgumentException("Can't parse line: expected separator at " + currentPosition + " (" + line + ")"); } currentPosition = separatorMatcher.end(); } } return matches; } private  StringUtil(); static String unquote(String quotedString); static String unescapeString(String escapedString); static List<Object> parseCSVLine(String line, String separatorRegex); static final String STRING_ESCAPE_SEQUENCE_REGEX; static final String QUOTED_STRING_REGEX; static final String SINGLEQUOTED_STRING_REGEX; static final String DOUBLE_REGEX; static final String DOUBLE_REGEX_WITH_NAN; }