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:
abc
pq"r,s"
{"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
,
is the comma we want to match (?=(?:[^\\"]*\\"[^\\"]*\\")*[^\\"]*$)
is a look ahead based on the topic discussion Java: splitting a comma-separated string but ignoring commas in quotes by Bart Kiers. (?=(?:[^{}]*{[^{}]*})*[^}]*$)
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.