all Python 3refunction
re.finditer()
search
Returns an iterator of Match objects for every match. It saves memory on large texts.
Syntax
re.finditer(pattern, string, flags=0)
The iterator is lazy and single-pass: a second loop over it sees nothing, so wrap it in list() if you need the results more than once. Unlike findall(), which hands back strings (or tuples of groups when the pattern has groups), finditer() yields real Match objects with start(), end() and group(n); matches come left to right and never overlap.
Examples
import re
for m in re.finditer(r'\d+', 'a1 b22 c333'):
print(m.group(), m.start()) # → ? три строки: «1 1», «22 4», «333 8»
matches = list(re.finditer(r'[A-Z]', 'Hello World'))
print(len(matches)) # → 2
print(matches[0].group()) # → H
See also
Open in the full glossarydocs.python.orgReport an inaccuracy