Supose I have this string:
a= "hello world hella warld"
and I want to match all coincidences whit the regex:
b='(?P<hel>hell[oa])\s*(?P<wrl>w[oa]rld)'
I can use re.findall(b,a) and get:
[('hello', 'world'),('hella','warld')]
but I really want to get:
[{'hel':'hello','wrl':'world'},{'hel':'hella','wrl':'warld'}]
Mi queston is there some native or easy way to get this in Python?
Second Question:
I wrote a function to get dictionaries:
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
but I’m no sure if it’s correct, can you guys see any mistake? or you know a better way to accomplish this?
You can use
finditerinstead offindallto get an iterator ofMatchObjects: