re.match
Check whether the beginning of the string matches a regular expression.
Python 3.13
re.match(pattern, string, flags=0)- =default value
- pattern
- The regular expression.str | Pattern
- string
- The string to check.str
- flags
- Flags, 0 (no flags) by default; combine several with |.intOptionalre.IGNORECASEre.MULTILINEre.DOTALLre.VERBOSEre.ASCII
Return value
A Match object if the pattern matches the start of the string, otherwise None.
Examples
Match at the start of a string
Python 3.13
import re
print(re.match(r'hello', 'hello world'))<re.Match object; span=(0, 5), match='hello'>
Match not at the start of a string
Python 3.13
import re
print(re.match(r'world', 'hello world'))None
Details
The match is checked only at the beginning of the string, even with re.MULTILINE. To search anywhere, use re.search; to check the whole string, use re.fullmatch.
