【Python】正则表达式

1. 正则表达式模式

1.1 字符

模式 描述
a, b, c, 1, 2, 3, - 一个普通字符
\d 一个数字字符
\D 一个非数字字符
\s 一个空白字符
\S 一个非空白字符
ab12 a, b, 1, 2中的一个字符
a-e a~e范围内的一个字符
\^a-e 不在a~e范围内的一个字符
. 一个非换行符字符

1.2 字符的重复次数

模式 描述
* 0次或多次
+ 1次或多次
? 0次或1次
{2} 2次
{2,5} 2~5次
{2,} 至少2次
{,5} 至多5次

1.3 组合

模式 描述
\d{3,4}a-e+ 多个小模式组合成大模式
|
() 分组

1.4 位置

模式 描述
^ 字符串起始位置
$ 字符串结束位置
\b 单词边界
\B 非单词边界
(?=...) 正向肯定预查,在任何匹配pattern的字符串开始处匹配查找字符串。 这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。 例如,"`Windows(?=95
(?!...) 正向否定预查,在任何不匹配pattern的字符串开始处匹配查找字符串。 这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。 例如"`Windows(?!95
(?<=...) 反向肯定预查,与正向肯定预查类拟,只是方向相反。 例如,"`(?<=95
(?<!...) 反向否定预查,与正向否定预查类拟,只是方向相反。 例如"`(?<!95

2. re模块

2.1 re.search和re.match

python 复制代码
re.search(pattern, string, flags=0)
# 扫描整个string查找正则表达式pattern产生匹配的第一个位置,返回Match对象
# 匹配失败返回None

re.match(pattern, string, flags=0)
# 和re.search一样,但必须从字符串的开始位置匹配
# 即便是MULTILINE多行模式,re.match()也只匹配字符串的开始位置,而不匹配每行开始

# flags:
# re.IGNORECASE或re.I    使匹配对大小写不敏感
# re.MULTILINE或re.M     多行匹配,影响^和$,使它们匹配字符串的每一行的开头和结尾
# flags可以用|进行组合

Match.group和Match.groups

python 复制代码
Match.group(num=0)
# 返回一个或者多个匹配的子组

m = re.match(r"(\S+) (\S+)", "I love coding.")
m.group(0)       # 整个匹配的字符串
'I love'
m.group(1)       # 第一个匹配的子组
'I'
m.group(2)       # 第二个匹配的子组
'love'
m.group(1, 2)    # 一个元组,包含第一个和第二个匹配的子组
('I', 'love')
python 复制代码
Match.groups()
# 返回一个元组,包含所有匹配的子组

m = re.match(r"(\d+)\.(\d+)", "3.1415")
m.groups()
('3', '1415')

2.2 re.findall和re.finditer

python 复制代码
re.findall(pattern, string, flags=0)
# 返回pattern在string中的所有非重叠匹配,以字符串列表或字符串元组列表的形式
# 如果没有组,返回与整个模式匹配的字符串列表
# 如果有且仅有一个组,返回与该组匹配的字符串列表
# 如果有多个组,返回与这些组匹配的字符串元组列表

re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
re.findall(r'(\w+)=(\d+)', 'set width=20 and height=10')
[('width', '20'), ('height', '10')]
python 复制代码
re.finditer(pattern, string, flags=0)
# 和re.findall一样,但是返回Match对象的迭代器

2.3 re.sub和re.subn

python 复制代码
re.sub(pattern, repl, string, count=0, flags=0)
# 用repl替换pattern匹配的字符串,返回替换完成的字符串
# count    模式匹配后替换的最大次数,默认0表示替换所有的匹配

phone = "2004-959-559 # 这是一个电话号码"
# 删除注释
num = re.sub(r'#.*$', "", phone)
print ("电话号码 : ", num)
# 电话号码 :  2004-959-559 
# 移除非数字的内容
num = re.sub(r'\D', "", phone)
print ("电话号码 : ", num)
# 电话号码 :  2004959559
python 复制代码
re.subn(pattern, repl, string, count=0, flags=0)
# 和re.sub一样,但是返回一个元组 (字符串, 替换次数)

2.4 re.split

python 复制代码
re.split(pattern, string, maxsplit=0, flags=0)
# 用pattern分割string,返回分割完成的字符串列表
# maxsplit    分割次数,默认为0,不限制次数

re.split(r'\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
re.split(r'(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
re.split(r'\W+', 'Words, words, words.', 1)
['Words', 'words, words.']
re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
['0', '3', '9']

2.5 re.compile

python 复制代码
re.compile(pattern, flags=0)
# 将正则表达式的样式编译为一个正则表达式对象,供match和search这两个函数使用

'''
prog = re.compile(pattern)
result = prog.match(string)
等价于
result = re.match(pattern, string)
'''
相关推荐
Patrick在香港2 分钟前
Python 抓 0.91 GB 香港法例:Agent 的进度该写进磁盘,不是写进上下文
爬虫·python·api·claude·香港
ClinicTech19 分钟前
实验室洗瓶机的清洗系统架构与自动化控制解析
java·python
洋洋不叫杨杨24 分钟前
Codex 实战:用 AI 写运维脚本
大数据·python
高级程序源33 分钟前
django大学生创新创业项目管理系统94923-计算机课程设计、毕业设计
javascript·vue.js·spring boot·后端·python·django·课程设计
wno7041 小时前
Spring Security短信验证码登录
java·python·spring
泡泡鱼(敲代码中)1 小时前
Python 容器类型学习笔记:列表、元组、字典、集合
开发语言·笔记·python·pycharm
别动我齐刘海1 小时前
ROS2 Jazzy + C++ 实战路线——基础学习2
c++·人工智能·vscode·python·学习·机器学习·机器人
AR-26710-2 小时前
Linux Day7——建组/用户、umask、Python脚本
linux·python
打工仔折腾 AI2 小时前
普通摄像头接入AI识别:绿联NAS部署Frigate监控实战
人工智能·后端·python·性能优化·ai agent 实战
kuuailetianzi2 小时前
第3篇:《变量、数据类型与基本运算》
python