Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

re.finditer

Yields match objects for successive non-overlapping matches.

Version: Python 3.14; repository image, production runtime not attested

Syntax

re.finditer(pattern, string, flags=0)

Yields match objects for successive non-overlapping matches.

Each match retains its start and exclusive end offsets; no matches produce an empty iterator.

Iterate over Match objects to retain spans and capture information without constructing a list of every result. Matches do not overlap and may have zero length. span() uses a start-inclusive, end-exclusive interval. Digits in the example occupy [1,2) and [4,6); the pattern z produces no objects.

import re
matches = re.finditer(r"(?=(aa))", "aaa")
assert [(m.span(), m.group(1)) for m in matches] == [((0, 0), "aa"), ((1, 1), "aa")]
assert list(matches) == []

Examples

import re
print([(m.group(), m.span()) for m in re.finditer(r"\d+", "a7 b23")])
print(list(re.finditer("z", "abc")))

Result

[('7', (1, 2)), ('23', (4, 6))]
[]

Iterate over Match objects to retain spans and capture information without constructing a list of every result. Matches do not overlap and may have zero length. span() uses a start-inclusive, end-exclusive interval. Digits in the example occupy [1,2) and [4,6); the pattern z produces no objects.

Sources