I would like to regex-match the string
"abc", "d,e" , "", ",f"
such that the groups abc
, d,e
, ``, and ,f
(without quotes) are separately matched.
With the group
"([^"]*)"
matching the "abc"
bits, I assumed the regex
(?:\s*"([^"]*)"\s*,)\s*"([^"]*)"\s*
would do the trick. However, it only matches abc
and d,e
.
I've created a toy example at regex101 that shows the behavior.
Any hints?
You'd want to make "following" group optional:
(?:\s*"([^"]*)"\s*)(?:,\s*"([^"]*)"\s*)?
Live demo
Cleaner RegEx:
/\s*"([^"]+)"(?:,\s*)?/g
Base on your last edit for including zero or more characters:
/\s*"([^"]*?)"(?:,\s*)?/g
Live demo
Almost similar to revo's answer, but here's my regex:
/(?:"([^"]*)")(?:\s*,\s*)?/g
Live Demo
This will get correct match for "abc" , "d,e" , "", ",f"
also.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With