Python re.fullmatch() Method
re.fullmatch() is a function in the Python re module used toFull matcha function for the entire string.
The difference between re.match() in Vue 2,re.fullmatch() Requires the entire string to match the regular expression, rather than matching only from the beginning.
Word meaning: full match means "full match".
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 match.
- flags:
- Type: integer (int, optional)
- Description: regular expression flags.
Function description
- Return Value: If the entire string matches, return a
matchobject; otherwise returnNone。 - Feature: Requires the string to match completely from start to finish; no extra characters are allowed.
Example
Let's master it through a series of examples re.fullmatch() through a series of examples from simple to complex.
Example 1: basic usage - full match
Example
import
re
Match the entire string completely
text
=
"hello"
result
=
re
.
fullmatch
(
r
'hello'
,
text
)
if
result:
(
"Full match succeeded:"
,
result.
group
(
)
)
Expected output:
完整匹配成功: hello
Code analysis:
re.fullmatch()Require the string "hello" to match the pattern "hello" completely.- The match succeeds and a match object is returned.
Example 2: when full matching fails
If the string has extra characters, the match fails.
Example
import
re
text
=
"hello world"
Try to fully match "hello"
result
=
re
.
fullmatch
(
r
'hello'
,
text
)
if
result:
(
"Match succeeded:"
,
result.
group
(
)
)
else
:
(
"Match failed: the string is not exactly 'hello'"
)
Expected output:
匹配失败: 字符串不是完全等于 'hello'
Code analysis:
- The string "hello world" contains more characters and cannot fully match "hello".
- In this case,
fullmatchReturnNone。
Example 3: using regular expression patterns
fullmatch The full regular expression syntax is supported.
Example
import
re
Fully match digits
text
=
"12345"
result
=
re
.
fullmatch
(
r
'\d+'
,
text
)
if
result:
(
"Digits matched:"
,
result.
group
(
)
)
Fully match letters
text2
=
"abc"
result2
=
re
.
fullmatch
(
r
'[a-z]+'
,
text2
)
if
result2:
(
"Letters matched:"
,
result2.
group
(
)
)
Expected output:
数字匹配: 12345
字母匹配: abc
Example 4: validating fixed formats
fullmatch Often used to check whether an input conforms to a specific format.
Example
import
re
Validate a phone number format
phone
=
"138-1234-5678"
result
=
re
.
fullmatch
(
r
'\d{3}-\d{4}-\d{4}'
,
phone
)
if
result:
(
f
"Phone number {phone} has a valid format"
)
else
:
(
f
"Phone number {phone} has an invalid format"
)
Validate email format
=
"test@example.com"
result2
=
re
.
fullmatch
(
r
'\w+@\w+.\w+'
,
)
if
result2:
(
f
"Email {email} has a valid format"
)
else
:
(
f
"Email {email} has an invalid format"
)
Expected output:
手机号 138-1234-5678 格式正确
邮箱 test@example.com 格式正确
Code analysis:
fullmatchWell suited to data validation scenarios.- The match succeeds only when the string fully conforms to the format.
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 a complete match
fullmatch_result
=
re
.
fullmatch
(
r
'hello'
,
text
)
(
"match result:"
,
match_result.
group
(
)
if
match_result
else
None
)
(
"fullmatch result:"
,
fullmatch_result.
group
(
)
if
fullmatch_result
else
None
)
(
"-"
20
)
Try to match the entire string completely
text2
=
"hello"
match_result2
=
re
.
match
(
r
'hello'
,
text2
)
fullmatch_result2
=
re
.
fullmatch
(
r
'hello'
,
text2
)
(
"match result:"
,
match_result2.
group
(
)
if
match_result2
else
None
)
(
"fullmatch result:"
,
fullmatch_result2.
group
(
)
if
fullmatch_result2
else
None
)
Expected output:
match 结果: hello
fullmatch 结果: None
--------------------
match 结果: hello
fullmatch 结果: hello
Code analysis:
re.match()Matching from the beginning of the string is enough; a complete match is not required.re.fullmatch()Requires the string to match completely from start to finish.
Example 6: using groups
Example
import
re
text
=
"2024-04-02"
Use groups to fully match a date
result
=
re
.
fullmatch
(
r
'(\d{4})-(\d{2})-(\d{2})'
,
text
)
if
result:
(
"Full match:"
,
result.
group
(
)
)
(
"Year:"
,
result.
group
(
1
)
)
(
"Month:"
,
result.
group
(
2
)
)
(
"Day:"
,
result.
group
(
3
)
)
Expected output:
完整匹配: 2024-04-02
年: 2024
月: 04
日: 02
Python re Module