Regular expression for "key = value" pairs

Vlastimil Brom vlastimil.brom at gmail.com
Wed Dec 22 11:43:15 EST 2010


2010/12/22 Ciccio <franapoli at gmail.com>:
> Hi all,
> suppose I have:
>
> s='a=b, c=d'
>
> and I want to extract sub-strings a,b,c and d from s (and in general from
> any longer list of such comma separated pairs).
> Some failed attempts:
>
> In [12]: re.findall(r'(.+)=(.+)', s)
> Out[12]: [('a=b, c', 'd')]
>
> [...]
> Thanks for your help,
> francesco.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hi,
I am not sure,  the regular expressions are best suited for this task,
but if you have a predictable simple parameter list (with ho
recursion, escaping commas or equal signs etc.), it might be viable;
how about e.g. this pattern?

>>> re.findall(r'([^=\s,]+)=([^=\s,]+)', s)
[('a', 'b'), ('c', 'd')]
>>>

vbr



More information about the Python-list mailing list