re.findall
Find all non-overlapping matches of a regular expression.
Python 3.13
re.findall(pattern, string, flags=0)- =default value
- pattern
- The regular expression.str | Pattern
- string
- The string to search in.str
- flags
- Flags, 0 (no flags) by default; combine several with |.intOptionalre.IGNORECASEre.MULTILINEre.DOTALLre.VERBOSEre.ASCII
Return value
A list of strings; with one group in the pattern, a list of its values; with several groups, a list of tuples. An empty list [] if there are no matches.
Examples
All numbers in a string
Python 3.13
import re
print(re.findall(r'\d+', '12 drummers, 11 pipers'))['12', '11']
Tuples of groups
Python 3.13
import re
print(re.findall(r'(\w+)=(\d+)', 'width=20 height=10'))[('width', '20'), ('height', '10')]Details
If the pattern has groups, the result contains the group values rather than the whole match, which often causes surprises. To group part of a pattern without capturing it, use (?:...). If you need match positions, re.finditer yields Match objects.
