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.compile() 方法 | 菜鸟教程
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.compile() 方法
将正则表达式模式预编译为正则表达式对象,可以重复使用,提高匹配效率。 单词释义: 基本语法与参数
语法格式re.compile(pattern, flags=0) 参数说明
函数说明
实例让我们通过一系列从简单到复杂的例子,彻底掌握 示例 1:基础用法 - 编译正则表达式实例
import re
# 编译一个正则表达式 pattern = re.compile(r'\d+') # 使用编译后的对象进行匹配 text = "我有3个苹果和5个橙子" result = pattern.findall(text) print("找到的数字:", result) 运行结果预期: 找到的数字: ['3', '5'] 代码解析:
示例 2:多次使用同一模式编译正则表达式的最大优势是可以重复使用,避免重复解析。 实例
import re
# 编译一个匹配邮箱的正则表达式 email_pattern = re.compile(r'\w+@\w+\.\w+') # 在多个文本中重复使用 texts = [ "联系 [email protected] 获取帮助", "邮箱: [email protected]", "发件人: [email protected]" ] for text in texts: result = email_pattern.search(text) if result: print(f"找到邮箱: {result.group()}") 运行结果预期: 找到邮箱: [email protected] 找到邮箱: [email protected] 找到邮箱: [email protected] 代码解析:
示例 3:使用标志位在编译时可以指定正则表达式标志,改变匹配行为。 实例
import re
# 编译时指定忽略大小写标志 pattern = re.compile(r'python', re.IGNORECASE) text = "Python is great, PYTHON is powerful" # 使用编译后的对象搜索 result = pattern.findall(text) print("找到:", result) 运行结果预期: 找到: ['Python', 'PYTHON'] 代码解析:
示例 4:使用分组编译后的对象可以使用分组来提取特定内容。 实例
import re
# 使用分组编译正则表达式 pattern = re.compile(r'(\d{3})-(\d{4})-(\d{4})') text = "张三: 138-1234-5678, 李四: 139-9876-5432" # 查找所有匹配 results = pattern.findall(text) for result in results: print(f"完整: {result[0]}-{result[1]}-{result[2]}") print(f" 第1组(前3位): {result[0]}") print(f" 第2组(中间4位): {result[1]}") print(f" 第3组(后4位): {result[2]}") 运行结果预期: 完整: 138-1234-5678 第1组(前3位): 138 第2组(中间4位): 1234 第3组(后4位): 5678 完整: 139-9876-5432 第1组(前3位): 139 第2组(中间4位): 9876 第3组(后4位): 5432 代码解析:
示例 5:编译对象的属性编译后的正则表达式对象有一些有用的属性。 实例
import re
# 编译正则表达式 pattern = re.compile(r'\d+', re.IGNORECASE | re.MULTILINE) print("模式:", pattern.pattern) print("标志:", pattern.flags) print("分组数:", pattern.groups) print("分组名:", pattern.groupindex) 运行结果预期: 模式: \d+
标志: 50
分组数: 1
分组名: {}
代码解析:
示例 6:与 re 模块函数对比实例
import re
text = "ABC123DEF" # 方法1: 直接使用 re 模块函数 result1 = re.search(r'\d+', text) # 方法2: 先编译,再使用编译后的对象 pattern = re.compile(r'\d+') result2 = pattern.search(text) print("re.search():", result1.group()) print("pattern.search():", result2.group()) # 两种方法结果相同,但编译后的对象可以重复使用 运行结果预期: re.search(): 123 pattern.search(): 123 代码解析:
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. |