On this page
re.finditer
Prev: re.findall | Up: re | Next: re.fullmatch
Rel: re.compile, re.error, re.escape, re.findall
Ref: docs.python.org — re.finditer
Tags: #public #python3 #stdlib #re
Import
import re
from re import finditer
Signature & Overview
re.finditer(pattern, string, flags=0)
Return an iterator over all non-overlapping matches in the string. For each match, the iterator returns a Match object.
Usage
import re
matches = [(m.group(), m.start(), m.end()) for m in re.finditer(r"\d+", "id=42 port=8080")]
assert matches == [("42", 3, 5), ("8080", 11, 15)]
Gotchas
- Lazy & richer than
findall(): Yieldsre.Matchobjects one by one with.span(),.groupdict(), and full match text regardless of capturing groups. - Memory-friendly on large text: Doesn't allocate a full list of matches up front.
REPL Inspect
from re import finditer
help(finditer)
Linked from
Related notes
- tuts/python3/stdlib_module/re/compile Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/error Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/escape Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/findall Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/fullmatch Linked here; links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/re/match shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/Match_class shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/Pattern shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/PatternError shared tags: #re, #stdlib