【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)
'''
相关推荐
@forever@16 小时前
【JAVA】LinkedList与链表
java·python·链表
程序员爱钓鱼17 小时前
Python编程实战:面向对象与进阶语法——类型注解与代码规范(PEP 8)
后端·python·ipython
程序员爱钓鱼17 小时前
Python实战:用高德地图API批量获取地址所属街道并写回Excel
后端·python·ipython
reasonsummer18 小时前
【教学类-97-06】20251105“葡萄”橡皮泥黏贴(小班主题《苹果与橘子》)
python
卖个几把萌19 小时前
【16】Selenium+Python 接管已打开谷歌浏览器
python·selenium·测试工具
像风一样的男人@19 小时前
python --两个文件夹文件名比对(yolo 图和label标注比对检查)
windows·python·yolo
lllsure19 小时前
【Python】Dict(字典)
开发语言·python
tianyuanwo20 小时前
Rust开发完全指南:从入门到与Python高效融合
开发语言·python·rust
如何原谅奋力过但无声21 小时前
TensorFlow 2.x常用函数总结(持续更新)
人工智能·python·tensorflow
程序员-小李21 小时前
基于 Python + OpenCV 的人脸识别系统开发实战
开发语言·python·opencv