SyntaxStudy
Sign Up
Python Intermediate 3 min read

re.finditer

re.finditer

re.finditer() returns an iterator of match objects for all matches, giving access to position information.

Example
import re

text = "cat bat hat"
for m in re.finditer(r"[cbh]at", text):
    print(f"{m.group()} at {m.start()}-{m.end()}")
# cat at 0-3
# bat at 4-7
# hat at 8-11
Pro Tip

finditer is memory-efficient for large texts — use instead of findall when you need positions.