When I was trying to answer this question: regex to split %ages and values in python I noticed that I had to re-order the groups from the result of findall. For example:
Take 3, based on a further clarification of the OP's intent in this comment.
Ashwin is correct that findall
does not preserve named capture groups (e.g. (?P
). finditer to the rescue! It returns the individual match objects one-by-one. Simple example:
data = """34% passed 23% failed 46% deferred"""
for m in re.finditer('(?P\w+)%\s(?P\w+)', data):
print( m.group('percentage'), m.group('word') )