Match `" abc "," d, e "," ",", f "` with groups

I would like the regular expression to match the string

"abc", "d,e" , "", ",f" 

so that the groups abc , d,e ,,,, and ,f (without quotes) are separately matched.

With a group

 "([^"]*)" 

matching the "abc" bit, I assumed that the regular expression

 (?:\s*"([^"]*)"\s*,)\s*"([^"]*)"\s* 

would do the trick. However, it corresponds only to abc and d,e .

I created an example toy in regex101 that shows the behavior.

Any clues?

+5
source share
2 answers

You want to make the optional "next" command:

 (?:\s*"([^"]*)"\s*)(?:,\s*"([^"]*)"\s*)? 

Live demo

Update # 1

RegEx Cleaner:

 /\s*"([^"]+)"(?:,\s*)?/g 

Update # 2

The basics of your last edit to include zero or more characters:

 /\s*"([^"]*?)"(?:,\s*)?/g 

Live demo

+3
source

Almost similar to revo's tip, but here is my regex:

 /(?:"([^"]*)")(?:\s*,\s*)?/g 

Live demo

It will also lead to a correct match for "abc" , "d,e" , "", ",f" .

+1
source

Source: https://habr.com/ru/post/1239050/


All Articles