Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The simplest way to parse a CSV in Java, period. < 30 lines, including comments and imports.
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SimplestCSVParser {
// private constructor -- utility class
private SimplestCSVParser(){}
// where the magic happens
public static ArrayList<String> lineOfCSV (String line) {
// regex string for parsing lines of CSV
String regex = "(?:,|\\n|^)((?:\"(?:(?:\"\")*(?:[^\"])*)*\")|(?:[^\",\\n]*)|(?:\\n|$))";
// compile regex pattern and apply it to the given line
Matcher matcher = Pattern.compile(regex).matcher(line);
// save all of the matches to this ArrayList
ArrayList<String> list = new ArrayList<>();
while (matcher.find()) list.add(matcher.group(1));
// If the first character of a line is a comma, the regex above will miss
// the fact that the first entry is null. So we add an empty String to the
// beginning of the ArrayList.
if (line.charAt(0) == ',') list.add(0, "");
// that's it!
return list;
}
}
/* If you wan't a code-golfed version:
----------
import java.util.*;
import java.util.regex.*;
public class G {
public ArrayList<String> o (String l) {
Matcher m = Pattern.compile("(?:,|\\n|^)((?:\"(?:(?:\"\")*(?:[^\"])*)*\")|(?:[^\",\\n]*)|(?:\\n|$))").matcher(l);
ArrayList<String> r = new ArrayList<>();
while (m.find()) r.add(m.group(1));
if (l.charAt(0) == ',') r.add(0, "");
return r;
}}
----------
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.