Looping through python regex matches
Python's re.findall
should work for you.
Live demo
import re
s = "ABC12DEF3G56HIJ7"
pattern = re.compile(r'([A-Z]+)([0-9]+)')
for (letters, numbers) in re.findall(pattern, s):
print(numbers, '*', letters)
It is better to use re.finditer
if your dataset is large because that reduces memory consumption (findall()
return a list of all results, finditer()
finds them one by one).
import re
s = "ABC12DEF3G56HIJ7"
pattern = re.compile(r'([A-Z]+)([0-9]+)')
for m in re.finditer(pattern, s):
print m.group(2), '*', m.group(1)