Python 3.7+reterm
re.Pattern
objects
A compiled regular expression (from re.compile) with match/search/findall/sub methods.
Syntax
re.compile(pattern) -> re.Pattern
You never instantiate it yourself: re.Pattern(...) is not a constructor, objects come only from re.compile, and the name exists for type hints and isinstance checks. A pattern remembers its string type — applying a str pattern to a bytes object (or the reverse) raises TypeError instead of quietly finding nothing. Useful attributes are pattern, flags and groupindex, the mapping of named groups.
Examples
import re
p = re.compile(r'\d+')
print(p.findall('a12b3')) # → ['12', '3']
print(p.search('year 2024').group()) # → 2024
print(p.match('year 2024')) # → None
print(p.sub('#', 'a12b3')) # → a#b#
print(isinstance(p, re.Pattern)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy