Reputation: 53
My CSV file has following text:
a, b, 0, "0, 1, 2", ""ab cd", 5", 10
My regex:
aColumnValue = dataRow.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
where aColumnValue is a String array.
This regex is failing since on the '"' before ab the regex closes and searches for the next token.
Please help find the correct regex.
The correct count of tokens should be: 6 and the actual tokens should be
a
b
0
0, 1, 2,
"ab cd", 5
10
Thanks in advance.
Upvotes: 0
Views: 84
Reputation: 3654
String input = "a, b, 0, \"0, 1, 2\", \"\"ab cd\", 5\", 10";
String[] parts = input.split(",(?=([^\"]*\"[^\"]*\")*(?![^\"]*\"))");
The parts
variable contains:
a
b
0
"0, 1, 2"
""ab cd", 5"
10
Likely you need to remove "
and spaces.
Upvotes: 0
Reputation: 115328
Do not parse CSV using regex. Use libraries that know to do this well. For example OpenCSV or Apache commons CSV
Upvotes: 3
Reputation: 136002
There may be more issues. You should use some CSV parser like opencsv http://opencsv.sourceforge.net/
Upvotes: 0