目前,我倾向于删除 CSV 行的字符串中的逗号。
这是我的期望
// (1) ",123,456," -> ",123456,"
// (2) ","abc,def"," -> ","abcdef","
// (3) ","123,456"," -> ","123456","
// (4) ","abcdef,"," -> ","abcdef","
我写了以下代码
String[] test = {
"\",123,456,\"",
"\",\"abc,def\",\"",
"\",\"123,456\",\"",
"\",\"abcdef,\",\""
};
final Pattern commaNotBetweenQuotes = Pattern.compile("(?<!\"),(?!\")");
for (String d : test) {
System.out.println("O : " + d);
String result = commaNotBetweenQuotes.matcher(d).replaceAll("");
System.out.println("R : " + result);
}
但是,我失败了(4)
这是我得到的输出
O : ",123,456,"
R : ",123456,"
O : ","abc,def","
R : ","abcdef","
O : ","123,456","
R : ","123456","
O : ","abcdef,","
R : ","abcdef,"," <-- we expect the comma after "f" being remove, as
it is inside string quote
我可以知道如何进一步改进这种正则表达式模式吗?
final Pattern commaNotBetweenQuotes = Pattern.compile("(?<!\"),(?!\")");
我从Java SE 和 Android 平台中的不同正则表达式结果中获取代码
我对模式的理解是
如果逗号的左侧和右侧没有双引号,请将其替换为空字符串。
我尝试使用
final Pattern commaNotBetweenQuotes = Pattern.compile("(?<!\"),(?!\")|(?<![\"0-9]),(?=\")");
有想法
如果逗号的左侧和右侧没有双引号,请将其替换为空字符串。
或者
如果逗号右侧有双引号,左侧有非数字/非双引号,则将其替换为空字符串。
然而,“解决方案”并不优雅。我真正想要的是,删除字符串文字中的逗号。删除整数内的逗号。保留逗号用作 CSV 分隔符。
尽量不要使用$1
,因为 Android 将使用 "null" 而不是 "" 来表示不匹配的组。