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

前端 未结 11 1553
广开言路
广开言路 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 06:12

    Rather than use lookahead and other crazy regex, just pull out the quotes first. That is, for every quote grouping, replace that grouping with __IDENTIFIER_1 or some other indicator, and map that grouping to a map of string,string.

    After you split on comma, replace all mapped identifiers with the original string values.

提交回复
热议问题