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

查看:139
本文介绍了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.)

上面的字符串应分成:

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"

换句话说:只有当逗号为零或偶数时,才会在逗号上拆分报价前面的报价

In other words: split on the comma only if that comma has zero, or an even number of quotes ahead of it.

或者,眼睛有点友善:

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在评论中提到的那样:

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天全站免登陆