Python 3.15+refunction
re.prefixmatch()
search
A new name for re.match: looks for a match only at the start of the string. The name says what the function does and is not confused with search or fullmatch.
Syntax
re.prefixmatch(pattern, string, flags=0)
re.match has long been confusing: its name suggests it finds a match anywhere or checks the whole string, while it only checks the beginning. prefixmatch is the same function under an honest name, and pattern objects have the same Pattern.prefixmatch method. Even with re.MULTILINE the match is tried only at the start of the string, not of every line. To search the whole string use re.search, to check the entire string use re.fullmatch. Code that must run on versions before 3.15 keeps using re.match: the old name is not deprecated.
Examples
import re
print(re.prefixmatch(r'\d+', '42 apples').group()) # → 42
print(re.prefixmatch(r'\d+', 'apples 42')) # → None
print(re.search(r'\d+', 'apples 42').group()) # → 42
See also
Open in the full glossarydocs.python.orgReport an inaccuracy