I have a string like:
'a, b, "c,d" e, f,'
I would like to split my string using the comma character as separator, but outside of the brackets. My result object set should contain the following elements:
a
b
"c,d" e
f
How can I achieve this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
yields
[ 'a', ' b', ' "c,d" e', ' f' ], which has extra spaces, but you can trim them.Edit: missed the
e… now fixed.Explanation:
(A|B)+prefersA(which is a quoted string) instead of B, which is a non-quoted part of the string. It matches"c,d", then because of the+it continues matchinge."[^"]+"matches a"followed by all the non-", followed by a single".[^,]matches any non-"/regex/gmakes global (matches all matches, not just the first)