Python Regex fullmatch()
Summary: in this tutorial, you’ll learn how to use the Python regex fullmatch() to match the whole string with a regular expression.
Introduction to the Python regex fullmatch function
The fullmatch() function returns a Match object if the whole string matches the search pattern of a regular expression, or None otherwise.
The syntax of the fullmatch() function is as follows:
re.fullmatch(pattern, string, flags=0)Code language: Python (python)
In this syntax:
patternspecifies a regular expression to match.stringspecifies the input string.flagsparameter is optional and defaults to zero. Theflagsparameter accepts one or more regex flags. Theflagsparameter changes how the regex engine matches the pattern.
Python regex fullmatch function example
The following example uses the fullmatch() function to validate an email address:
import reemail = 'no-reply@pythontutorial.net'
pattern = r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}'
match = re.fullmatch(pattern, email)
if match is not None:
print(f'The email "{match.group()}" is valid')
else:
print(f'The email "{email}"" is not valid')
Code language: Python (python)
Output:
The email "no-reply@pythontutorial.net" is validCode language: Python (python)
The following defines a function that uses the fullmatch() function to validate an email address. It returns True if the email is valid or raises a ValueError exception otherwise:
import redef is_email(s: str) -> bool:
pattern = r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}'
if re.fullmatch(pattern, s) is None:
raise ValueError(f'The {s} is not a valid email address')
return True
Code language: Python (python)
And you can use the is_email() function to validate an email like this:
if __name__ == '__main__':
try:
if is_email('no-reply@pythontutorial'):
print('The email is valid')
except ValueError as e:
print(e)Code language: Python (python)
Output:
The no-reply@pythontutorial is not a valid email addressCode language: Python (python)
Python regex fullmatch vs match
Both fullmatch() and match() functions return a Match object if they find a match.
The fullmatch() function matches the whole string with a pattern while the match() function only finds a match at the beginning of the string. See the following example:
import res = 'Python 3'
pattern = 'Python'
# fullmatch
match = re.fullmatch(pattern, s)
if match is not None:
print('fullmatch:', match.group())
# search
match = re.match(pattern, s)
if match is not None:
print('match:', match.group())
Code language: Python (python)
Output:
match: PythonCode language: Python (python)
In this example, the fullmatch() returns None because the pattern Python only matches the beginning of the string, not the whole string.
On the other hand, the match() function matches the pattern at the beginning of the string and returns the match.
Python fullmatch vs. search
Both fullmatch() and search() functions return a Match object if they find a match of a pattern in a string. However, the fullmatch() matches the whole string while the search() matches anywhere in the string.
For example:
import res = 'Python 3'
pattern = '\d'
# fullmatch
match = re.fullmatch(pattern,s)
if match is not None:
print(match.group())
# search
match = re.search(pattern,s)
if match is not None:
print(match.group()) # 3
Code language: Python (python)
Output:
3Code language: Python (python)
In this example, the pattern \d matches a single digit. The fullmatch() function returns None because the whole string 'Python 3' doesn’t match.
However, the search() function returns a match because it could find the digit 3 at the end of the string.
Summary
- Use the Python regex
fullmatch()function to check if the whole string matches a pattern of a regular expression.