Python中re模块常用方法总结分析
作者:季布, 时间:2022-10-27 14:40:20
re.findall()
在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。
语法格式:
re.findall(pattern, string, flags=0)
参数:pattern
:匹配字符串的正则。string
:待匹配的字符串。
示例:
需求:查找字符串中的所有数字
result = re.findall(r'\d+','1python2hello3')
print(result)
结果
['1', '2', '3']
result = re.findall(r'p%','helloP%ython')
print(result)
结果
[] #因为要匹配的字符串P% 和正则p%不匹配,所以返回空
result = re.findall(r'p%','helloP%ython',re.I)
print(result)
结果
[P%]
re.sub()
语法:
re.sub(pattern, repl, string, count=0, flags=0)
参数:pattern
: 正则中的模式字符串。repl
: 替换的字符串,也可为一个函数。string
: 要被查找替换的原始字符串。count
: 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配。
result = re.sub(r'python','world','hellopython')
print(result)
结果
helloworld
repl 参数是一个函数
#需求:将匹配到结果中小于4的替换成8,大于4的替换成9
import re
def modify(value):
matched = value.group()
if int(matched) <= 4:
return '8'
else:
return '9'
str = 'ab12cd34ef567'
result = re.sub('\d', modify, str)
print(result )
结果
ab88cd88ef999
使用场景:可以对匹配成功要替换的部分做操作
re.compile
当我们在Python中使用正则表达式时,re模块内部:
1)编译正则表达式,如果正则表达式的字符串本身不合法,会报错;
2)用编译后的正则表达式去匹配字符串。
那么如果一个正则表达式要重复使用几千次,出于效率的考虑,我们是不是应该先把这个正则先预编译好,接下来重复使用时就不再需要编译这个步骤了,直接匹配,提高我们的效率
compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用。
语法格式为:
re.compile(pattern[, flags])
参数:pattern
: 一个字符串形式的正则表达式flags
: 可选,表示匹配模式,比如忽略大小写,多行模式等,
str = 'ab12cd34ef567'
info = re.compile(r'([a-z]{2})(\d{2})')
result = info.match(str).group()
result1 = info.match(str).groups()
print(result)
print(result1)
结果
ab12
('ab', '12')
re.match
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
语法
re.match(pattern, string, flags=0)
参数:pattern
:匹配的正则表达式string
:要匹配的字符串。flags
: 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等
str = 'ab12cd34ef567'
info = re.compile(r'([a-z]{2})(\d{2})')
result = info.match(str).group()
result1 = info.match(str).groups()
print(result)
print(result1)
结果
ab12
('ab', '12')
str1 = '0b12cd34ef567'
result1 = info.match(str1).group()
print(result1)
结果: 会报错
AttributeError: 'NoneType' object has no attribute 'group'
re.search
re.search 扫描整个字符串并返回第一个成功的匹配。
参数:pattern
:匹配的正则表达式string
:要匹配的字符串。flags
:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等
str = 'ab12cd34ef567'
info = re.compile(r'([0-9]{2})')
result = info.search(str).group()
print(result)
结果
12
re.match与re.search的区别
re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;
re.search匹配整个字符串,直到找到一个匹配。
re.split
split 方法按照能够匹配的子串将字符串分割后返回列表
语法
re.split(pattern, string[, maxsplit=0, flags=0])
参数:pattern
: 匹配的正则表达式string
:要匹配的字符串。flags
:标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等
str = 'abc def g hi'
result = re.split('\W+',str)
print(result)
结果
['abc', 'def', 'g', 'hi']
来源:https://blog.csdn.net/weixin_47906106/article/details/120054698
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
hover悬停放大的翻页效果
Golang模拟令牌桶进行对访问的限流方式
mysql load data infile 的用法(40w数据 用了3-5秒导进mysql)
Python计算双重差分模型DID及其对应P值使用详解
![](https://img.aspxhome.com/file/2023/5/68335_0s.jpg)
mysql与mssql的md5加密语句
ASP.NET中MVC从后台控制器传递数据到前台视图的方式
PHP实现将浏览历史页面网址保存到cookie的方法
Python采集C站高校信息实战示例
WEB2.0网页制作标准教程(1)选择什么样的DOCTYPE
python绘制地震散点图
![](https://img.aspxhome.com/file/2023/5/82545_0s.jpg)
为PHP模块添加SQL SERVER2012数据库的步骤详解
![](https://img.aspxhome.com/file/2023/7/126227_0s.jpg)
block 和 inline 的区别是?
![](https://img.aspxhome.com/file/UploadPic/200912/8/203330321-54s.gif)
python求最大值,不使用内置函数的实现方法
浅谈PHP的$_SERVER[SERVER_NAME]
Python线程之如何解决共享变量问题
![](https://img.aspxhome.com/file/2023/0/70700_0s.png)
发一个较复杂的字符串截取函数
使用Python编写一个简单的tic-tac-toe游戏的教程
python爬虫的一个常见简单js反爬详解
python中split()函数的用法详解
![](https://img.aspxhome.com/file/2023/0/134660_0s.png)