Java:分割逗号分隔的字符串但忽略引号中的逗号

标签 java regex string

我有一个隐约像这样的字符串:

foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy"

我想用逗号分隔 - 但我需要忽略引号中的逗号。我怎样才能做到这一点?似乎正则表达式方法失败了;我想当我看到报价时我可以手动扫描并输入不同的模式,但使用预先存在的库会很好。 (编辑:我想我的意思是已经是 JDK 一部分的库,或者已经是 Apache Commons 等常用库的一部分。)

上面的字符串应该分割成:

foo
bar
c;qual="baz,blurb"
d;junk="quux,syzygy"

注意:这不是 CSV 文件,它是包含在具有较大整体结构的文件中的单个字符串

最佳答案

尝试:

public class Main { 
    public static void main(String[] args) {
        String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
        String[] tokens = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
        for(String t : tokens) {
            System.out.println("> "+t);
        }
    }
}

输出:

> foo
> bar
> c;qual="baz,blurb"
> d;junk="quux,syzygy"

换句话说:仅当逗号前面有零个或偶数个引号时才在逗号上拆分

或者,对眼睛更友好一点:

public class Main { 
    public static void main(String[] args) {
        String line = "foo,bar,c;qual=\"baz,blurb\",d;junk=\"quux,syzygy\"";
        
        String otherThanQuote = " [^\"] ";
        String quotedString = String.format(" \" %s* \" ", otherThanQuote);
        String regex = String.format("(?x) "+ // enable comments, ignore white spaces
                ",                         "+ // match a comma
                "(?=                       "+ // start positive look ahead
                "  (?:                     "+ //   start non-capturing group 1
                "    %s*                   "+ //     match 'otherThanQuote' zero or more times
                "    %s                    "+ //     match 'quotedString'
                "  )*                      "+ //   end group 1 and repeat it zero or more times
                "  %s*                     "+ //   match 'otherThanQuote'
                "  $                       "+ // match the end of the string
                ")                         ", // stop positive look ahead
                otherThanQuote, quotedString, otherThanQuote);

        String[] tokens = line.split(regex, -1);
        for(String t : tokens) {
            System.out.println("> "+t);
        }
    }
}

产生的结果与第一个示例相同。

编辑

正如 @MikeFHay 在评论中提到的:

I prefer using Guava's Splitter, as it has saner defaults (see discussion above about empty matches being trimmed by String#split(), so I did:

Splitter.on(Pattern.compile(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))

关于Java:分割逗号分隔的字符串但忽略引号中的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57394162/

相关文章:

java - 如何防止我的类被反编译

java - 如何在多个浏览器中运行 Selenium 测试以使用 Java 进行跨浏览器测试?

Java 定时触发器

Java 对大型抽象列表的二分查找

regex - 查找文件夹中编号最大的文件名

正则表达式匹配方括号一次

Python 数组解析

php - 你如何使 preg_replace 捕获大写 (php)?

python - 如何将不带引号的字符串转换为python中的字典

Python:返回完整单词而不仅仅是字符串的特定部分(正则表达式)