all Python 3refunction
re.findall()
search
Returns the list of every match of the pattern in the string. With one group it lists that group's values, with several — tuples.
Syntax
re.findall(pattern, string, flags=0)
The classic trap is groups: with exactly one group in the pattern you get that group's contents rather than the whole matches, so use the non-capturing (?:...) when the parentheses are only for grouping. Matches are non-overlapping and scanned left to right, and the entire list is built at once — for large texts prefer re.finditer, which yields Match objects lazily and carries their positions.
Examples
import re
re.findall(r'\d+', 'a1 b22 c333') # → ['1', '22', '333']
re.findall(r'[aeiou]', 'hello world') # → ['e', 'o', 'o']
re.findall(r'(\w+)=(\w+)', 'x=1 y=2') # → [('x', '1'), ('y', '2')]
re.findall(r'cat|dog', 'cat and dog') # → ['cat', 'dog']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy