简体   繁体   中英

Regex to split a string on the basis of comma in Java

I have a string from a csv file which I want to split. The string may contain a comma within a double quote or within a JSON. For example if the string is:

abc, pq"r,s", {"one":1, "two":2}

The regex should split it into three tokens as:

  1. abc
  2. pq"r,s"
  3. {"one":1, "two":2}

I have tried this regex .

The regex reads like this: (?x)[,](?=([^"]*"[^"]*")*[^"]*$)

Can anyone please suggest a right regex?

Here is the regex that works on your example abc, pq"r,s", {"one":1, "two":2} :

,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)(?=(?:[^{}]*{[^{}]*})*[^}]*$)

Or check out this regex101 example

There are three parts on this regex

  1. , is the comma we want to match
  2. (?=(?:[^\\"]*\\"[^\\"]*\\")*[^\\"]*$) is a look ahead based on the topic discussion Java: splitting a comma-separated string but ignoring commas in quotes by Bart Kiers.
  3. (?=(?:[^{}]*{[^{}]*})*[^}]*$) is an adapted look ahead to handle { ... } .

Not sure if it will work on other examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM