GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Python re.fullmatch() 方法 | 菜鸟教程
Python 3 教程
Python3 教程
Python3 简介
Python3 环境搭建
Python3 Pycharm
Python3 VScode
Python3 基础语法
Python3 基本数据类型
Python3 数据类型转换
Python3 解释器
Python3 注释
Python3 运算符
Python3 数字(Number)
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环语句
Python3 编程第一步
Python3 推导式
Python3 迭代器与生成器
Python3 with
Python3 函数
Python3 lambda
Python3 装饰器
Python3 数据结构
Python3 模块
Python __name__
Python3 输入和输出
Python3 File
Python3 OS
Python3 错误和异常
Python3 面向对象
Python3 命名空间/作用域
Python 虚拟环境的创建
Python 类型注解
Python3 标准库概览
Python3 实例
Python 测验
Python3 高级教程Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQtPython re.fullmatch() 方法
与 单词释义: 基本语法与参数
语法格式re.fullmatch(pattern, string, flags=0) 参数说明
函数说明
实例让我们通过一系列例子掌握 示例 1:基础用法 - 完整匹配实例
import re
# 完整匹配整个字符串 text = "hello" result = re.fullmatch(r'hello', text) if result: print("完整匹配成功:", result.group()) 运行结果预期: 完整匹配成功: hello 代码解析:
示例 2:完整匹配失败的情况如果字符串有多余的字符,匹配会失败。 实例
import re
text = "hello world" # 尝试完整匹配 "hello" result = re.fullmatch(r'hello', text) if result: print("匹配成功:", result.group()) else: print("匹配失败: 字符串不是完全等于 'hello'") 运行结果预期: 匹配失败: 字符串不是完全等于 'hello' 代码解析:
示例 3:使用正则表达式模式
实例
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()) 运行结果预期: 数字匹配: 12345 字母匹配: abc 示例 4:验证固定格式
实例
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} 格式错误") 运行结果预期: 手机号 138-1234-5678 格式正确 邮箱 [email protected] 格式正确 代码解析:
示例 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) 运行结果预期: match 结果: hello fullmatch 结果: None -------------------- match 结果: hello fullmatch 结果: hello 代码解析:
示例 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)) 运行结果预期: 完整匹配: 2024-04-02 年: 2024 月: 04 日: 02
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |