How can I use a regex to split (or match) this string:
a=1,b=2,c=3,4,5,d=6,e=x,y,z
The basic form is name=value,name=value where value could contain commas and name is always alphanumeric.
I’m trying to end up with:
a=1
b=2
c=3,4,5
d=6
e=x,y,z
My first thought was that the grammar was ambiguous since the values contain commas, but I think it should be doable since name doesn’t contain =.
This is close, but matches the trailing comma to each value and doesn’t match the final z:
(?<name>\w+)
\s*=\s*
(?<value>
\S
(?:
,
|
.[^=]
)*
)
Produces these matches:
a=1,
b=2,
c=3,4,5,
d=6,
e=x,y,
Any regex wizards on here?
You can just split on
commawhich is followed by analphabetand then=, using a look-ahead assertion. You can use the below regex for split: –