Pockets in Python's finditer method
The finditer method can also use pockets. Let's see in practice. Let's say we have a string:
txt = 'aaaa 123 bbbb 456'
Let's place all the numbers that are in the line into pockets. And when iterating over the objects with a loop, we will output the first match (it will be considered the zero pocket), as well as its elements, which in turn will also be scattered across the pockets:
res = re.finditer('(\d)(\d)', txt)
for el in res:
print(el[0], el[1], el[2])
After executing the code, the zero pocket will be output, i.e. the entire substring, as well as the first and second characters from this substring:
'12 1 2'
'45 4 5'
Given a string:
txt = 'aaa 123 bbb 456 987'
Spread all substrings with numbers into three pockets. And output them using a loop.