2

目前,我倾向于删除 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" 而不是 "" 来表示不匹配的组。

4

2 回答 2

2

描述

要替换卡在字符串中间的所有逗号,请使用以下内容,空捕获组(\b)应避免 android 出现问题,如果反向引用$#不匹配,则语言插入空字符而不是什么:

正则表达式:((?:",\d|\d,")|",")|(\b),

用。。。来代替:$1

在此处输入图像描述

输入

",123,456," 
","abc,def","
","123,456"," 
","abcdef,","

输出

",123456," 
","abcdef","
","123456"," 
","abcdef","

免责声明

这假设您要保留的逗号都被引号包围"alpha","beta","1234"

于 2013-06-14T03:16:52.313 回答
0

您还可以在您的字符串中找到第二次出现的 , 然后将其替换为“”。这里有一些例子:

于 2013-06-14T04:05:39.000 回答