2009-10-30 42 views

回答

2

这工作:

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

它看起来像一本字典+元组Python语法,所以eval()函数也将工作(如果您信任的来源!)

0

取决于你需要什么就有什么能比正则表达式其他的解决办法。对于这个特殊的例子中,下面的代码将工作:

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

还有parser模块,它可能是这样的任务非常有用。