Capturing repeating subpatterns in Python regex

re module doesn’t support repeated captures (regex supports it):

>>> m = regex.match(r'([.\w]+)@((\w+)(\.\w+)+)', '[email protected]')
>>> m.groups()
('yasar', 'webmail.something.edu.tr', 'webmail', '.tr')
>>> m.captures(4)
['.something', '.edu', '.tr']

In your case I’d go with splitting the repeated subpatterns later. It leads to a simple and readable code e.g., see the code in @Li-aung Yip’s answer.

Leave a Comment