Regex to parse "a = 1, b = 2, c = 3,4,5, d = 6, e = x, y, z"
How can I use regex to split (or match) this string:
a=1,b=2,c=3,4,5,d=6,e=x,y,z
Basic form name=value,name=value
, where the value can contain commas and the name is always alphanumeric.
I am trying in the end:
a=1
b=2
c=3,4,5
d=6
e=x,y,z
My first thought was that the grammar was ambiguous as the values ββcontain commas, but I think this should be doable as the name does not contain =
.
This is close, but matches the trailing comma of each value and doesn't match the final z
:
(?<name>\w+)
\s*=\s*
(?<value>
\S
(?:
,
|
.[^=]
)*
)
Produces the following matches:
a=1,
b=2,
c=3,4,5,
d=6,
e=x,y,
Are there any regex wizards here?
source to share
If your regex engine supports forward-looking statements , you can try the following:
(\w)=(\w(,\w(?!=))*)
Note. I used \w
to make it shorter. If you only want to match only alphanumeric characters, replace \w
instead[a-zA-Z0-9]
Works successfully on regexpal
EDIT: busted by Rohit Jain
source to share