re.match() / re.search() / re.fullmatch()
Functions for pattern matching strings using regular expressions. You can check whether a string matches a specific pattern, or extract the matched portion.
Syntax
import re # Check if the pattern matches at the beginning of the string. m = re.match(pattern, string) # Search the entire string for the first match. m = re.search(pattern, string) # Check if the entire string matches the pattern exactly. m = re.fullmatch(pattern, string) # Retrieve matched strings from the match object. m.group() # Returns the entire match. m.group(1) # Returns the first capture group. m.groups() # Returns all groups as a tuple.
Function List
| Function / Method | Description |
|---|---|
| re.match(pattern, string) | Checks if the pattern matches at the beginning of the string. Does not match if the pattern appears elsewhere. |
| re.search(pattern, string) | Searches the entire string and finds the first position where the pattern matches. Matches even if it is not at the beginning. |
| re.fullmatch(pattern, string) | Matches only if the entire string matches the pattern exactly. |
| m.group() | Returns the entire matched string. Pass an integer to return the string of that capture group. |
| m.groups() | Returns the strings of all capture groups as a tuple. |
| m.group('name') | Returns the string of a named capture group. |
| m.start() / m.end() | Returns the start and end indices of the matched range. |
Sample Code
import re
# match() checks from the beginning of the string.
m = re.match(r'\d+', '123abc')
if m:
print(m.group()) # Prints '123'.
m2 = re.match(r'\d+', 'abc123')
print(m2) # Prints 'None' because the string does not start with a digit.
# search() searches the entire string.
m3 = re.search(r'\d+', 'abc123def')
if m3:
print(m3.group()) # Prints '123'.
# fullmatch() requires an exact match for the entire string.
m4 = re.fullmatch(r'\d+', '12345')
print(bool(m4)) # Prints 'True'.
m5 = re.fullmatch(r'\d+', '123abc')
print(bool(m5)) # Prints 'False' (contains letters in the middle).
# Use capture groups to extract parts of the match.
m6 = re.search(r'(\d{4})-(\d{2})-(\d{2})', 'Today is 2025-04-15.')
if m6:
print(m6.group()) # Prints '2025-04-15'.
print(m6.group(1)) # Prints '2025'.
print(m6.group(2)) # Prints '04'.
print(m6.groups()) # Prints "('2025', '04', '15')".
# Use named capture groups.
pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
m7 = re.search(pattern, '2025-04-15')
if m7:
print(m7.group('year')) # Prints '2025'.
print(m7.group('month')) # Prints '04'.
# Validate an email address format.
email = 'user@example.com'
if re.fullmatch(r'[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}', email):
print('Valid email address.')
Notes
re.match() checks only the beginning of the string, while re.search() searches the entire string. Use re.search() when you want to find a pattern regardless of its position. Both functions return None on failure, so always check the return value with an if statement before accessing groups.
It is recommended to use Python raw strings (r"...") for regular expression patterns. Raw strings let you write backslashes without escaping them, making patterns easier to read.
Even when working with strings that contain Unicode characters such as Japanese, no additional configuration like PHP's /u modifier is needed — Python's regular expressions support Unicode by default.
To retrieve all matches, see re.findall() / re.finditer(). For replacement and splitting, see re.sub() / re.split() / re.compile().
If you find any errors or copyright issues, please contact us.