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?

+3


source to share


3 answers


You can simply divide by comma

followed by alphabet

then =

using a forward-looking statement. You can use the following regex for split: -



",(?=[a-zA-Z0-9]=)");

      

+3


source


In your final match, s ,|.[^=]

.[^=]

means any character followed by a character that is not =

. However z

, nothing follows. Instead, you must make any character followed by a non- =

OR end of the line:

,
|
.(?:[^=]|$)

      



I have verified this works using php preg_match_all

.

0


source


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

0


source







All Articles