String Tokenizer : split string by comma and ignore comma in double quotes

后端 未结 6 2054
闹比i
闹比i 2021-01-18 03:58

I have a string like below -

value1, value2, value3, value4, \"value5, 1234\", value6, value7, \"value8\", value9, \"value10, 123.23\"

6条回答
  •  逝去的感伤
    2021-01-18 04:44

    You just need one line and the right regex:

    String[] values = input.replaceAll("^\"", "").split("\"?(,|$)(?=(([^\"]*\"){2})*[^\"]*$) *\"?");
    

    This also neatly trims off the wrapping double quotes for you too, including the final quote!

    Note: Interesting edge case when the first term is quoted required an extra step of trimming the leading quote using replaceAll().

    Here's some test code:

    String input= "\"value1, value2\", value3, value4, \"value5, 1234\", " +
        "value6, value7, \"value8\", value9, \"value10, 123.23\"";
    String[] values = input.replaceAll("^\"", "").split("\"?(,|$)(?=(([^\"]*\"){2})*[^\"]*$) *\"?");
    for (String s : values)
    System.out.println(s);
    

    Output:

    value1, value2
    value3
    value4
    value5, 1234
    value6
    value7
    value8
    value9
    value10, 123.23
    

提交回复
热议问题