I want to parse a line from a CSV(comma separated) file, something like this:
Bosh,Mark,mark@gmail.com,"3, Institute","83, 1, 2",1,21
I have to parse the file, and instead of the commas between the apostrophes I wanna have ‘;’, like this:
Bosh,Mark,mark@gmail.com,"3; Institute","83; 1; 2",1,21
I use the following Java code but it doesn’t parse it well:
Pattern regex = Pattern.compile("(\"[^\\]]*\")");
Matcher matcher = regex.matcher(line);
if (matcher.find()) {
String replacedMatch = matcher.group();
String gr1 = matcher.group(1);
gr1.trim();
replacedMatch = replacedMatch.replace(",", ";");
line = line.replace(matcher.group(), replacedMatch);
}
the output is:
Bosh,Mark,mark@gmail.com,"3; Institute";"83; 1; 2",1,21
anyone have any idea how to fix this?
This is my solution to replace
,inside quote to;. It assumes that if"were to appear in a quoted string, then it is escaped by another". This property ensures that counting from start to the current character, if the number of quotes"is odd, then that character is inside a quoted string.Although I cannot find any case that will fail the method of replace the matched group like you did in
line = line.replace(matcher.group(), replacedMatch);, I feel safer to rebuild the string from scratch.