Question about regular expression

Emile van Sebille emile at
Wed Sep 30 20:50:12 CEST 2015

On 9/30/2015 11:34 AM, massi_srb at wrote:
> Hi everyone,
> firstly the description of my problem. I have a string in the following form:
> s = "name1 name2(1) name3 name4 (1, 4) name5(2) ..."
> that is a string made up of groups in the form 'name' (letters only) plus possibly a tuple containing 1 or 2 integer values. Blanks can be placed between names and tuples or not, but they surely are placed beween two groups. I would like to process this string in order to get a dictionary like this:
> d = {
>      "name1":(0, 0),
>      "name2":(1, 0),
>      "name3":(0, 0),
>      "name4":(1, 4),
>      "name5":(2, 0),
> }
> I guess this problem can be tackled with regular expressions,

Stop there!  :)

I'd use string functions.  If you can control the string output to drop 
the spaces and always output in namex(a,b)<space>namey(c,d)... format, 
try starting with

 >>> "name1 name2(1) name3 name4(1,4) name5(2)".split()
['name1', 'name2(1)', 'name3', 'name4(1,4)', 'name5(2)']

then create the dict from the result.


More information about the Python-list mailing list