3
Supose我有这个字符串:是否有像re.findall函数,但返回字典,而不是元组?
a= "hello world hella warld"
,我想匹配所有的巧合白衣正则表达式:
b='(?P<hel>hell[oa])\s*(?P<wrl>w[oa]rld)'
我可以使用re.findall(B,A),并得到:
[('hello', 'world'),('hella','warld')]
,但我真的想:
[{'hel':'hello','wrl':'world'},{'hel':'hella','wrl':'warld'}]
Mi queston是否有一些本地或简单的方法来获得Python的?
第二个问题:
我写了一个函数来获取字典:
def findalldict(regex,line):
matches = []
match = 1
c = line
while match != None and len(c)>1:
match =re.search(regex,c)
if match:
matches.append(match.groupdict())
c =c[match.end():]
return matches
,但我不知道这是否是正确的,你们可以看到任何错误?或者你知道一个更好的方法来完成这个?