tags:

views:

112

answers:

3

How to parse the string " {'result':(Boolean, MessageString)} " using Python regular expressions to get Boolean and the MessageString separated into variables?

+1  A: 

It looks like a dictionary+tuple in Python syntax, so eval() would also work (if you trust the source!!!)

Wim
A: 

Depending on what you need there can be other solution than regular expression. For this particular example the following code will work:

>>> compile("{'result':(Boolean, MessageString)}", '<expr>', 'eval').co_names
('Boolean', 'MessageString')

There is also parser module which might be useful for such tasks.

Denis Otkidach
+2  A: 

This works:

>>> x = re.search('\((.*),\s*(.*)\)', " {'result':(Boolean, MessageString)} ")
>>> x.group(1)
'Boolean'
>>> x.group(2)
'MessageString'
Graeme Perrow