Python Re Fullmatch
## Python3.x Python re.fullmatch() Method
[ Python re Module](#)
* * *
`re.fullmatch()` is a function in Python's `re` module used for **full matching** the entire string.
Unlike `re.match()`, `re.fullmatch()` requires the entire string to completely match the regular expression, not just from the beginning.
**Word Meaning**: `full match` means complete matching.
* * *
## Basic Syntax and Parameters
`re.fullmatch()` is a module-level function, called directly through the `re` module.
### Syntax Format
re.fullmatch(pattern, string, flags=0)
### Parameter Description
* **pattern**οΌ
* Type: string (str)
* Description: The regular expression pattern to match.
* **string**οΌ
* Type: string (str)
* Description: The string to be matched.
* **flags**οΌ
* Type: integer (int, optional)
* Description: Regular expression flags.
### Function Description
* **Return Value**: If the entire string matches, returns a `match` object; otherwise returns `None`.
* **Feature**: Requires the string to match completely from start to end, with no extra characters allowed.
* * *
## Examples
Let's master the usage of `re.fullmatch()` through a series of examples.
### Example 1: Basic Usage - Full Match
## Example
import re
# Full match the entire string
text ="hello"
result =re.fullmatch(r'hello', text)
if result:
print("Full match successful:", result.group())
**Expected Output:**
Full match successful: hello
**Code Explanation:**
* `re.fullmatch()` requires the string "hello" to completely match the pattern "hello".
* On successful match, returns a match object.
### Example 2: Case of Full Match Failure
If the string has extra characters, the match will fail.
## Example
import re
text ="hello world"
# Attempt to fully match "hello"
result =re.fullmatch(r'hello', text)
if result:
print("Match successful:", result.group())
else:
print("Match failed: The string is not exactly equal to 'hello'")
**Expected Output:**
Match failed: The string is not exactly equal to 'hello'
**Code Explanation:**
* The string "hello world" contains more characters and cannot fully match "hello".
* In this case, `fullmatch` returns `None`.
### Example 3: Using Regular Expression Patterns
`fullmatch` supports complete regular expression syntax.
## Example
import re
# Full match numbers
text ="12345"
result =re.fullmatch(r'd+', text)
if result:
print("Number match:", result.group())
# Full match letters
text2 ="abc"
result2 =re.fullmatch(r'+', text2)
if result2:
print("Letter match:", result2.group())
**Expected Output:**
Number match: 12345Letter match: abc
### Example 4: Validating Fixed Formats
`fullmatch` is often used to validate whether input conforms to specific formats.
## Example
import re
# Validate phone number format
phone ="138-1234-5678"
result =re.fullmatch(r'd{3}-d{4}-d{4}', phone)
if result:
print(f"Phone number {phone} format is correct")
else:
print(f"Phone number {phone} format is incorrect")
# Validate email format
email="test@example.com"
result2 =re.fullmatch(r'w+@w+.w+',email)
if result2:
print(f"Email {email} format is correct")
else:
print(f"Email {email} format is incorrect")
**Expected Output:**
Phone number 138-1234-5678 format is correctEmail test@example.com format is correct
**Code Explanation:**
* `fullmatch` is very suitable for data validation scenarios.
* Only when the string completely conforms to the format will the match succeed.
### Example 5: Comparing match and fullmatch
## Example
import re
text ="hello world"
# match only matches from the beginning
match_result =re.match(r'hello', text)
# fullmatch requires full match
fullmatch_result =re.fullmatch(r'hello', text)
print("match result:", match_result.group()if match_result else None)
print("fullmatch result:", fullmatch_result.group()if fullmatch_result else None)
print("-" * 20)
# Try full matching the entire string
text2 ="hello"
match_result2 =re.match(r'hello', text2)
fullmatch_result2 =re.fullmatch(r'hello', text2)
print("match result:", match_result2.group()if match_result2 else None)
print("fullmatch result:", fullmatch_result2.group()if fullmatch_result2 else None)
**Expected Output:**
match result: hello fullmatch result: None-------------------- match result: hello fullmatch result: hello
**Code Explanation:**
* `re.match()` only needs to match from the beginning of the string, not requiring a full match.
* `re.fullmatch()` requires the entire string to match completely from start to end.
### Example 6: Using Groups
## Example
import re
text ="2024-04-02"
# Use groups to fully match the date
result =re.fullmatch(r'(d{4})-(d{2})-(d{2})', text)
if result:
print("Full match:", result.group())
print("Year:", result.group(1))
print("Month:", result.group(2))
print("Day:", result.group(3))
**Expected Output:**
Full match: 2024-04-02Year: 2024Month: 04Day: 02
* * Python re Module](#)
YouTip