Python正则表达式打印出字符串的各个部分

时间:2014-08-29 02:06:35

标签: python regex string

这是我的代码(对于testString的冗长道歉,我不太确定如何格式化它):

testString = """{"id":"174833828503074873","classid":"217769142","instanceid":"0","amount":"1","pos":1},"174833828503117736":{"id":"174833828503117736","classid":"217769142","instanceid":"574274862","amount":"1","pos":2}asdf234sadf"""
sections = re.findall(r'{.*}', testString)
for section in sections:
    print section

我想提取以下两个字符串: {"id":"174833828503074873","classid":"217769142","instanceid":"0","amount":"1","pos":1} {"id":"174833828503117736","classid":"217769142","instanceid":"574274862","amount":"1","pos":2}

但是,当我打印这些部分时,它会提取第一个和第二个项目并将它们组合成一个字符串:

{"id":"174833828503074873","classid":"217769142","instanceid":"0","amount":"1","pos":1},"174833828503117736":{"id":"174833828503117736","classid":"217769142","instanceid":"574274862","amount":"1","pos":2}

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

你需要在?之后添加*量词来做一个不情愿的匹配(最短的匹配)。基本上*只会进行贪婪的匹配。

sections = re.findall(r'{.*?}', testString)

DEMO

>>> import re
>>> testString = """{"id":"174833828503074873","classid":"217769142","instanceid":"0","amount":"1","pos":1},"174833828503117736":{"id":"174833828503117736","classid":"217769142","instanceid":"574274862","amount":"1","pos":2}asdf234sadf"""
>>> sections = re.findall(r'{.*?}', testString)
>>> for section in sections:
...     print section
... 
{"id":"174833828503074873","classid":"217769142","instanceid":"0","amount":"1","pos":1}
{"id":"174833828503117736","classid":"217769142","instanceid":"574274862","amount":"1","pos":2}