sameer oak
sameer oak

Reputation: 53

parsing CSV file in java with " in the CSV file

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

Answers (3)

m-szalik
m-szalik

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

AlexR
AlexR

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

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

There may be more issues. You should use some CSV parser like opencsv http://opencsv.sourceforge.net/

Upvotes: 0

Related Questions