The purpose of life is not to win. The purpose of life is to grow and to share. When you come to look back on all that you have done in life, you will get more satisfaction from the pleasure you have brought into other people's lives than you will from the times that you outdid and defeated them.

CSV Parser in Java

public class CSVParser {

public static final String CSV_REGEX_PATTERN = "\"([^\"]+?)\",?([^,]+),?,";
public static final String TSV_REGEX_PATTERN = "\t{1}";
public static Pattern csvRE;
public static Pattern tsvRE;

static {
csvRE = Pattern.compile(CSV_REGEX_PATTERN);
tsvRE = Pattern.compile(TSV_REGEX_PATTERN);
}

private List parse(String line) {

List list = new ArrayList();
Matcher m = csvRE.matcher(line);

// For each field
while (m.find()) {
String match = m.group();
if (match == null)
break;
//this will remove trailing , - comment this out if we dont want that affect
if (match.endsWith(",")) {
match = match.substring(0, match.length() - 1);
}
if (match.startsWith("\"")) {
match = match.substring(1, match.length () - 1);
}
if (match.length() == 0)
match = null;
list.add(match);
}

return list;
}

}

No comments: