Named pockets in finditer method in Python
When using the finditer method, named pockets can be used:
txt = 'aaaa 123 bbbb 456'
res = re.finditer('(?P<test1>\d)(?P<test2>\d)', txt)
for el in res:
print(el[0], el['test1'], el['test2'])
Result of code execution:
'12 1 2'
'45 4 5'
Given a string:
txt = '456 aaa 123 b2bb 987'
Find all substrings containing only digits and distribute them into two named pockets. Print them using a loop.