I am having troubles to get groups for each key-pair value from a string. The input string is something like:
sample1= "key1.1=val1,key2=2,key3=3,key4=last"
expected 4 groups:
key1.1=val1
key2=2
key3=3
key4=last
but also possible
sample2= "key1.1=val11,key2.1=2,1,key33=3,key44=last"
note that here in sample2
, the key2.1
has the value 2,1
expected 4 groups: key1.1=val11
key2.1=2,1
key33=3
key44=last
I am unable to get all the groups and specially to deal with the case where the delimiter ,
between the pairs can also be part of the value of one key.
I am trying something like:
(?:^|,)([^=,] )=([^=,] )(?:,|$)
CodePudding user response:
You can split on commas that are followed by one or more characters other than commas, followed by an equal sign. That is, split on matches of the regular expression
,(?=[^,] =)
The method or function you use to split the string on matches of this expression depends on the language you are using, which you have not specified. In Ruby, for example, you would write
"key1.1=val11,key2.1=2,1,key33=3,key44=last".split(/,(?=[^,] =)/)
#=> ["key1.1=val11", "key2.1=2,1", "key33=3", "key44=last"]
The expression can be broken down as follows.
, # match a comma
(?= # begin a positive lookahead
[^,] # match one or more characters other than commas
= # match an equal sign
) # end positive lookahead