Regular expression to match comma separated list of key=value where value can contain commas

前端 未结 5 689
一生所求
一生所求 2020-12-20 15:54

I have a naive \"parser\" that simply does something like:
[x.split(\'=\') for x in mystring.split(\',\')]

However mystring can be something like<

5条回答
  •  攒了一身酷
    2020-12-20 16:28

    Assuming that the name of the key never contains ,, you can split at , when the next sequence without , and = is succeeded by =.

    re.split(r',(?=[^,=]+=)', inputString)
    

    (This is the same as my original solution. I expect re.split to be used, rather than re.findall or str.split).

    The full solution can be done in one-liner:

    [re.findall('(.*?)=(.*)', token)[0] for token in re.split(r',(?=[^,=]+=)', inputString)]
    

提交回复
热议问题