Python re.fullmatch() Method
re.fullmatch()is a Pythonremodule used tofully matcha function for the entire string.
andre.match()Unlike,re.fullmatch()it requires the entire string to fully match the regular expression, not just match from the beginning.
Word Meaning: full matchmeans full match.
Basic Syntax and Parameters
re.fullmatch()is a module-level function, directly called viarethe 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
matchobject; otherwise returnsNone。 - Feature: Requires the string to match completely from beginning to end, no extra characters allowed.
Examples
Let's master through a series of examplesre.fullmatch()the usage of.
Example 1: Basic Usage - Full Match
Example
# Fully match the entire string
text = "hello"
result = re.fullmatch(r'hello', text)
if result:
print("Full match successful:", result.group())
Expected output:
完整匹配成功: hello
Code explanation:
re.fullmatch()Requires the string "hello" to fully match the pattern "hello".- The match succeeds, returning a match object.
Example 2: Case of Full Match Failure
If the string has extra characters, the match will fail.
Example
text = "hello world"
# Try to fully match "hello"
result = re.fullmatch(r'hello', text)
if result:
print("Match successful:", result.group())
else:
print("Match failed: string is not exactly equal to 'hello'")
Expected output:
匹配失败: 字符串不是完全等于 'hello'
Code explanation:
- The string "hello world" contains more characters, so it cannot fully match "hello".
- In this case,
fullmatchreturnsNone。
Example 3: Using Regular Expression Patterns
fullmatchSupports full regular expression syntax.
Example
# Fully match digits
text = "12345"
result = re.fullmatch(r'\d+', text)
if result:
print("Digit match:", result.group())
# Fully match letters
text2 = "abc"
result2 = re.fullmatch(r'[a-z]+', text2)
if result2:
print("Letter match:", result2.group())
Expected output:
数字匹配: 12345 字母匹配: abc
Example 4: Validating Fixed Formats
fullmatchOften used to validate whether input conforms to a specific format.
Example
# 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 = "[email protected]"
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:
手机号 138-1234-5678 格式正确 邮箱 [email protected] 格式正确
Code explanation:
fullmatchVery suitable for data validation scenarios.- Only when the string completely conforms to the format does the match succeed.
Example 5: Comparing match and fullmatch
Example
text = "hello world"
# match only matches from the beginning
match_result = re.match(r'hello', text)
# fullmatch requires a 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 to fully match 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 结果: hello fullmatch 结果: None -------------------- match 结果: hello fullmatch 结果: hello
Code explanation:
re.match()It only needs to match from the beginning of the string; a full match is not required.re.fullmatch()Requires the entire string to fully match from beginning to end.
Example 6: Using Groups
Example
text = "2024-04-02"
# Use groups to fully match a 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:
完整匹配: 2024-04-02 年: 2024 月: 04 日: 02
Other Extensions
Python re Module