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/57164232/

相关文章:

java - 使用字符串缓冲区从 ArrayList 中获取单行或双行字符串

java - 我什么时候应该使用流?

python - 使用正则表达式删除包含数字的双引号

Javascript regExp - 替换函数应该决定不替换匹配的字符串以让其他带括号的子匹配字符串与匹配项一起使用

c - 需要字符串初始化建议

C#回调接收UTF8字符串

java - 在 JTextArea 上调度关键事件不会移动插入符

java - 如何将 fxml 加载器的位置设置为另一个包中的 fxml

Java 5 HTML 转义以防止 XSS

javascript - 用于选择并替换为标题大小写值的正则表达式