Python 3.4+refunction
re.fullmatch()
search
Checks whether the pattern matches the whole string. Returns a Match or None.
Syntax
re.fullmatch(pattern, string, flags=0)
When the question is whether the entire string fits the pattern, reach for fullmatch(): match() anchors only at the start, so a digits pattern happily accepts something like 123abc. Bolting on a $ by hand is not the same thing — $ also matches just before a trailing newline, so a string ending in a line break slips through while fullmatch() rejects it.
Examples
import re
re.fullmatch(r'\d+', '12345') # → <Match '12345'>
re.fullmatch(r'\d+', '123abc') # → None
re.fullmatch(r'[a-z]+', 'hello') # → <Match 'hello'>
re.fullmatch(r'[a-z]+', 'Hello') # → None
re.fullmatch(r'[a-z]+', 'Hello', re.IGNORECASE) # → <Match 'Hello'>
See also
Open in the full glossarydocs.python.orgReport an inaccuracy