Looping through python regex matches Looping through python regex matches python python

Looping through python regex matches


Python's re.findall should work for you.

Live demo

import res = "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 res = "ABC12DEF3G56HIJ7"pattern = re.compile(r'([A-Z]+)([0-9]+)')for m in re.finditer(pattern, s):    print m.group(2), '*', m.group(1)