Python re.fullmatch() 方法
re.fullmatch() 是 Python re 模块中用于完整匹配整个字符串的函数。
与 re.match() 不同,re.fullmatch() 要求整个字符串完全匹配正则表达式,而不是只从开头匹配。
单词释义: full match 是完整匹配的意思。
基本语法与参数
re.fullmatch() 是一个模块级函数,直接通过 re 模块调用。
语法格式
re.fullmatch(pattern, string, flags=0)
参数说明
- pattern:
- 类型:字符串 (str)
- 描述:要匹配的正则表达式模式。
- string:
- 类型:字符串 (str)
- 描述:被匹配的字符串。
- flags:
- 类型:整数 (int, 可选)
- 描述:正则表达式标志。
函数说明
- 返回值: 如果整个字符串匹配,返回
match对象;否则返回None。 - 特点: 要求字符串从头到尾完全匹配,不允许有多余的字符。
实例
让我们通过一系列例子掌握 re.fullmatch() 的用法。
示例 1:基础用法 - 完整匹配
实例
import re
# 完整匹配整个字符串
text = "hello"
result = re.fullmatch(r'hello', text)
if result:
print("完整匹配成功:", result.group())
# 完整匹配整个字符串
text = "hello"
result = re.fullmatch(r'hello', text)
if result:
print("完整匹配成功:", result.group())
运行结果预期:
完整匹配成功: hello
代码解析:
re.fullmatch()要求字符串 "hello" 完全匹配模式 "hello"。- 匹配成功,返回 match 对象。
示例 2:完整匹配失败的情况
如果字符串有多余的字符,匹配会失败。
实例
import re
text = "hello world"
# 尝试完整匹配 "hello"
result = re.fullmatch(r'hello', text)
if result:
print("匹配成功:", result.group())
else:
print("匹配失败: 字符串不是完全等于 'hello'")
text = "hello world"
# 尝试完整匹配 "hello"
result = re.fullmatch(r'hello', text)
if result:
print("匹配成功:", result.group())
else:
print("匹配失败: 字符串不是完全等于 'hello'")
运行结果预期:
匹配失败: 字符串不是完全等于 'hello'
代码解析:
- 字符串 "hello world" 包含更多字符,无法完整匹配 "hello"。
- 这种情况下,
fullmatch返回None。
示例 3:使用正则表达式模式
fullmatch 支持完整的正则表达式语法。
实例
import re
# 完整匹配数字
text = "12345"
result = re.fullmatch(r'\d+', text)
if result:
print("数字匹配:", result.group())
# 完整匹配字母
text2 = "abc"
result2 = re.fullmatch(r'[a-z]+', text2)
if result2:
print("字母匹配:", result2.group())
# 完整匹配数字
text = "12345"
result = re.fullmatch(r'\d+', text)
if result:
print("数字匹配:", result.group())
# 完整匹配字母
text2 = "abc"
result2 = re.fullmatch(r'[a-z]+', text2)
if result2:
print("字母匹配:", result2.group())
运行结果预期:
数字匹配: 12345 字母匹配: abc
示例 4:验证固定格式
fullmatch 常用于验证输入是否符合特定格式。
实例
import re
# 验证手机号格式
phone = "138-1234-5678"
result = re.fullmatch(r'\d{3}-\d{4}-\d{4}', phone)
if result:
print(f"手机号 {phone} 格式正确")
else:
print(f"手机号 {phone} 格式错误")
# 验证邮箱格式
email = "[email protected]"
result2 = re.fullmatch(r'\w+@\w+\.\w+', email)
if result2:
print(f"邮箱 {email} 格式正确")
else:
print(f"邮箱 {email} 格式错误")
# 验证手机号格式
phone = "138-1234-5678"
result = re.fullmatch(r'\d{3}-\d{4}-\d{4}', phone)
if result:
print(f"手机号 {phone} 格式正确")
else:
print(f"手机号 {phone} 格式错误")
# 验证邮箱格式
email = "[email protected]"
result2 = re.fullmatch(r'\w+@\w+\.\w+', email)
if result2:
print(f"邮箱 {email} 格式正确")
else:
print(f"邮箱 {email} 格式错误")
运行结果预期:
手机号 138-1234-5678 格式正确 邮箱 [email protected] 格式正确
代码解析:
fullmatch非常适合用于数据验证场景。- 只有字符串完全符合格式时才匹配成功。
示例 5:对比 match 和 fullmatch
实例
import re
text = "hello world"
# match 只从开头匹配
match_result = re.match(r'hello', text)
# fullmatch 要求完整匹配
fullmatch_result = re.fullmatch(r'hello', text)
print("match 结果:", match_result.group() if match_result else None)
print("fullmatch 结果:", fullmatch_result.group() if fullmatch_result else None)
print("-" * 20)
# 试试完整匹配整个字符串
text2 = "hello"
match_result2 = re.match(r'hello', text2)
fullmatch_result2 = re.fullmatch(r'hello', text2)
print("match 结果:", match_result2.group() if match_result2 else None)
print("fullmatch 结果:", fullmatch_result2.group() if fullmatch_result2 else None)
text = "hello world"
# match 只从开头匹配
match_result = re.match(r'hello', text)
# fullmatch 要求完整匹配
fullmatch_result = re.fullmatch(r'hello', text)
print("match 结果:", match_result.group() if match_result else None)
print("fullmatch 结果:", fullmatch_result.group() if fullmatch_result else None)
print("-" * 20)
# 试试完整匹配整个字符串
text2 = "hello"
match_result2 = re.match(r'hello', text2)
fullmatch_result2 = re.fullmatch(r'hello', text2)
print("match 结果:", match_result2.group() if match_result2 else None)
print("fullmatch 结果:", fullmatch_result2.group() if fullmatch_result2 else None)
运行结果预期:
match 结果: hello fullmatch 结果: None -------------------- match 结果: hello fullmatch 结果: hello
代码解析:
re.match()从字符串开头匹配即可,不要求完整匹配。re.fullmatch()要求整个字符串从头到尾完全匹配。
示例 6:使用分组
实例
import re
text = "2024-04-02"
# 使用分组完整匹配日期
result = re.fullmatch(r'(\d{4})-(\d{2})-(\d{2})', text)
if result:
print("完整匹配:", result.group())
print("年:", result.group(1))
print("月:", result.group(2))
print("日:", result.group(3))
text = "2024-04-02"
# 使用分组完整匹配日期
result = re.fullmatch(r'(\d{4})-(\d{2})-(\d{2})', text)
if result:
print("完整匹配:", result.group())
print("年:", result.group(1))
print("月:", result.group(2))
print("日:", result.group(3))
运行结果预期:
完整匹配: 2024-04-02 年: 2024 月: 04 日: 02
Python re 模块
点我分享笔记