我想从 CSV(逗号分隔)文件中解析一行,如下所示:
Bosh,Mark,mark@gmail.com,"3, Institute","83, 1, 2",1,21
我必须解析文件,而不是我想要的撇号之间的逗号';',就像这样:
Bosh,Mark,mark@gmail.com,"3; Institute","83; 1; 2",1,21
我使用以下 Java 代码,但它不能很好地解析它:
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);
}
输出是:
Bosh,Mark,mark@gmail.com,"3; Institute";"83; 1; 2",1,21
有人知道如何解决这个问题吗?