On this page
re.findall
Prev: re.escape | Up: re | Next: re.finditer
Rel: re.compile, re.error, re.escape, re.finditer
Ref: docs.python.org — re.findall
Tags: #public #python3 #stdlib #re
Import
import re
from re import findall
Signature & Overview
re.findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
Usage
import re
assert re.findall(r"\d+", "a1 b22 c333") == ["1", "22", "333"]
# With 2+ capturing groups, findall returns a list of tuples
assert re.findall(r"(\w+)=(\d+)", "x=1 y=2") == [("x", "1"), ("y", "2")]
Gotchas
- Return shape changes with capturing groups: 0 groups → list of full matches; 1 group → list of group-1 strings only; 2+ groups → list of tuples. Use non-capturing
(?:...)if you want full matches. - Non-overlapping matches only: For overlapping matches, use a lookahead like
r"(?=(\d\d))".
REPL Inspect
from re import findall
help(findall)
Linked from
- tuts/python3/stdlib_module/re
- tuts/python3/stdlib_module/re/compile
- tuts/python3/stdlib_module/re/error
- tuts/python3/stdlib_module/re/escape
- tuts/python3/stdlib_module/re/finditer
- tuts/python3/stdlib_module/re/fullmatch
- tuts/python3/stdlib_module/re/match
- tuts/python3/stdlib_module/re/Match_class
- tuts/python3/stdlib_module/re/Pattern
- tuts/python3/stdlib_module/re/PatternError
- tuts/python3/stdlib_module/re/purge
- tuts/python3/stdlib_module/re/RegexFlag
- tuts/python3/stdlib_module/re/search
- tuts/python3/stdlib_module/re/split
- tuts/python3/stdlib_module/re/sub
- tuts/python3/stdlib_module/re/subn
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/finditer 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/fullmatch links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/match links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/Match_class links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/Pattern links to this page; shared tags: #re, #stdlib
- tuts/python3/stdlib_module/re/PatternError links to this page; shared tags: #re, #stdlib