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 模块 | 菜鸟教程
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 模块Python 的 正则表达式(Regular Expression,简称 regex 或 regexp)是一种强大的工具,用于匹配、搜索和操作文本。 通过 为什么使用 re 模块?在处理文本时,我们经常需要查找特定的模式或替换某些字符。例如,验证电子邮件地址、提取网页中的链接、或者格式化文本。手动编写代码来完成这些任务可能会非常繁琐,而正则表达式提供了一种简洁且高效的方式来解决这些问题。 re 模块的基本用法1. 导入 re 模块在使用 import re 2. 常用的 re 模块函数2.1
|
| 方法 | 说明 | 示例 |
|---|---|---|
re.compile(pattern) |
预编译正则表达式(提升复用性能) | pat = re.compile(r'\d+') |
re.search(pattern, string) |
搜索字符串中第一个匹配项 | re.search(r'\d+', 'a1b2') → 匹配 '1' |
re.match(pattern, string) |
从字符串起始位置匹配 | re.match(r'\d+', '123a') → 匹配 '123' |
re.fullmatch(pattern, string) |
整个字符串完全匹配 | re.fullmatch(r'\d+', '123') → 匹配 '123' |
re.findall(pattern, string) |
返回所有非重叠匹配的列表 | re.findall(r'\d+', 'a1b22c') → ['1', '22'] |
re.finditer(pattern, string) |
返回所有匹配的迭代器(含位置信息) | for m in re.finditer(r'\d+', 'a1b2'): print(m.group()) |
re.sub(pattern, repl, string) |
替换匹配项 | re.sub(r'\d+', 'X', 'a1b2') → 'aXbX' |
re.split(pattern, string) |
按匹配项分割字符串 | re.split(r'\d+', 'a1b2c') → ['a', 'b', 'c'] |
re.escape() |
转义特殊字符 | re.escape("C:\\Users\\test.txt")
|
re.purge() |
清除缓存 | re.purge()
|
Match)方法/属性| 方法/属性 | 说明 | 示例 |
|---|---|---|
group() |
返回整个匹配的字符串 | m.group() → 'abc' |
group(n) |
返回第n个捕获组的内容 | m = re.search(r'(\d)(\d)', '12'); m.group(1) → '1' |
groups() |
返回所有捕获组的元组 | m.groups() → ('1', '2') |
start()/end() |
匹配的起始/结束位置 | m.start() → 0 |
span() |
返回匹配范围 (start, end) |
m.span() → (0, 2) |
| 元字符 | 说明 | 示例匹配 |
|---|---|---|
. |
匹配任意字符(除换行符) | a.c → 'abc' |
\d |
匹配数字 | \d+ → '123' |
\D |
匹配非数字 | \D+ → 'abc' |
\w |
匹配单词字符(字母、数字、下划线) | \w+ → 'Ab_1' |
\W |
匹配非单词字符 | \W+ → '!@#' |
\s |
匹配空白字符(空格、制表符等) | \s+ → ' \t' |
\S |
匹配非空白字符 | \S+ → 'abc' |
[] |
字符集合 | [A-Za-z] → 任意字母 |
^ |
匹配字符串开头 | ^\d+ → 开头的数字 |
$ |
匹配字符串结尾 | \d+$ → 结尾的数字 |
* |
匹配前一个字符0次或多次 | a* → '', 'aaa' |
+ |
匹配前一个字符1次或多次 | a+ → 'a', 'aaa' |
? |
匹配前一个字符0次或1次 | a? → '', 'a' |
{m,n} |
匹配前一个字符m到n次 | a{2,3} → 'aa', 'aaa' |
| |
或操作 | cat|dog → 'cat' 或 'dog' |
() |
捕获分组 | (\d+) → 提取数字 |
flags 参数)| 标志 | 说明 | 示例 |
|---|---|---|
re.IGNORECASE (re.I) |
忽略大小写 | re.search(r'abc', 'ABC', re.I) |
re.MULTILINE (re.M) |
多行模式(影响 ^ 和 $) |
re.findall(r'^\d+', '1\n2', re.M) → ['1', '2'] |
re.DOTALL (re.S) |
让 . 匹配包括换行符的所有字符 |
re.search(r'a.*b', 'a\nb', re.S) |
re.ASCII |
让 \w, \W 等仅匹配ASCII字符 |
re.search(r'\w+', 'こん', re.ASCII) → 无匹配 |
re.VERBOSE (re.X) |
允许正则中添加注释和空格 | re.compile(r'''\d+ # 匹配数字''', re.X) |
1. 提取邮箱地址
2. 替换日期格式
3. 多条件匹配
4. 分割复杂字符串
原始字符串:建议正则表达式使用原始字符串(r'\d'),避免转义符冲突。
贪婪匹配:默认贪婪匹配(如 .* 会匹配到最长可能),需用 .*? 实现非贪婪匹配。
性能优化:频繁使用的正则应优先用 re.compile() 预编译。
回溯问题:复杂正则可能导致性能问题(如嵌套量词 (a+)+)。
取消