Java: splitting a comma-separated string but ignoring commas in quotes

前端 未结 11 1536
广开言路
广开言路 2020-11-21 05:16

I have a string vaguely like this:

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

that I want to split by commas -- but I need to igno

11条回答
  •  梦如初夏
    2020-11-21 05:48

    Try:

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

    Output:

    > 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.

    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.

    EDIT

    As mentioned by @MikeFHay in the comments:

    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(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))
    

提交回复
热议问题