Java:拆分逗号分隔的字符串但忽略引号中的逗号 [英] Java: splitting a comma-separated string but ignoring commas in quotes

查看:51
本文介绍了Java:拆分逗号分隔的字符串但忽略引号中的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样模糊的字符串:

I have a string vaguely like this:

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

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

that I want to split by commas -- but I need to ignore commas in quotes. How can I do this? Seems like a regexp approach fails; I suppose I can manually scan and enter a different mode when I see a quote, but it would be nice to use preexisting libraries. (edit: I guess I meant libraries that are already part of the JDK or already part of a commonly-used libraries like Apache Commons.)

上面的字符串应该拆分为:

the above string should split into:

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

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

note: this is NOT a CSV file, it's a single string contained in a file with a larger overall structure

推荐答案

尝试:

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"

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

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

Or, a bit friendlier for the eyes:

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);
        }
    }
}

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

which produces the same as the first example.

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

As mentioned by @MikeFHay in the comments:

我更喜欢使用 Guava 的 Splitter,因为它有更合理的默认值(请参阅上面关于 String#split() 修剪空匹配的讨论,所以我做了:

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:拆分逗号分隔的字符串但忽略引号中的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆