删除CSV行的字符串引号中的逗号

时间:2013-06-14 02:22:49

标签: java android regex

目前,我倾向于删除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("(?<!\"),(?!\")");

我从Different regular expression result in Java SE and Android platform

获取代码

我对模式的理解是

  

如果逗号在其左侧和右侧没有双引号,   用空字符串替换它。

我尝试使用

     final Pattern commaNotBetweenQuotes = Pattern.compile("(?<!\"),(?!\")|(?<![\"0-9]),(?=\")");

有想法

  

如果逗号在其左侧和右侧没有双引号,   用空字符串替换它。

     

OR

     

如果逗号右边有双引号,非数字/非双引号   在左边引用,用空字符串替换它。

然而,“解决方案”并不优雅。我真正想要的是,删除字符串文字中的逗号。删除整数内的逗号。保留用作CSV分隔符的逗号。

尽量不要使用$1,因为对于不匹配的群组,Android会使用“null”代替“”。

2 个答案:

答案 0 :(得分:2)

描述

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

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

替换为:$1

enter image description here

输入

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

输出

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

声明

这假定您要保留的逗号都被"alpha","beta","1234"

等引号括起来

答案 1 :(得分:0)

您还可以在String中找到第二次出现,然后用“”替换它。这里有一些例子: