京公网安备 11010802034615号
经营许可证编号:京B2-20210330
Python正则表达式使用经典实例
本文给大家总结了17种python正则表达式使用经典实例,非常不错具有参考借鉴价值,下面列出Python正则表达式的几种匹配用法,具体内容如下所示:
1.测试正则表达式是否匹配字符串的全部或部分
regex=ur"" #正则表达式
if re.search(regex, subject):
do_something()
else:
do_anotherthing()
2.测试正则表达式是否匹配整个字符串
regex=ur"\Z" #正则表达式末尾以\Z结束
if re.match(regex, subject):
do_something()
else:
do_anotherthing()
3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group()
else:
result = ""
5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group(1)
else:
result = ""
6. 获取有名组所匹配的子串(Get the part of a string matched by a named group)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
result = match.group"groupname")
else:
result = ""
7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)
result = re.findall(regex, subject)
8.遍历所有匹配的子串(Iterate over all matches in a string)
for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations)
reobj = re.compile(regex)
10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched)
reobj = re.compile(regex)
if reobj.search(subject):
do_something()
else:
do_anotherthing()
11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely)
reobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束
if reobj.match(subject):
do_something()
else:
do_anotherthing()
12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
# match start: match.start()
# match end (exclusive): atch.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group()
else:
result = ""
14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group(1)
else:
result = ""
15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group)
reobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group("groupname")
else:
result = ""
16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string)
reobj = re.compile(regex)
result = reobj.findall(subject)
17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string)
reobj = re.compile(regex)
for match in reobj.finditer(subject):
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
字符串替换
1.替换所有匹配的子串
#用newstring替换subject中所有与正则表达式regex匹配的子串
result = re.sub(regex, newstring, subject)
2.替换所有匹配的子串(使用正则表达式对象)
reobj = re.compile(regex)
result = reobj.sub(newstring, subject)
字符串拆分
1.字符串拆分
result = re.split(regex, subject)
2.字符串拆分(使用正则表示式对象)
reobj = re.compile(regex)
result = reobj.split(subject)
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
初中毕业想考证,先确认“我能不能报”,再比较证书价值。学历门槛不够的证书,宣传再多也与你无关;能报但对应不上岗位的证书, ...
2026-11-02市场岗确有一批报考不设专业限制的证书,但“不限专业”只解决能不能报,真正决定值不值的是三件事:对应市场岗哪条子路径、招聘 ...
2026-11-02“大学生必考证书有哪些”这个问题,本身就藏着一个需要先拆掉的假设:似乎存在一份人人适用的必考清单。实际上,除了少数与毕业 ...
2026-11-02“最好考的一级证书”这个问题,最容易踩的坑不是选错证书,而是没先弄清“一级”在目标证书体系里到底指什么。有的体系里,一级 ...
2026-11-02“职业证书哪个含金量高”这个问题,很难用一张榜单回答。原因不复杂:证书的“含金量”取决于你拿它做什么。想进入必须持证的行 ...
2026-11-02“证件含金量排名”是搜索量很高的词,但直接给你一份“十大证书排行榜”的文章,基本可以跳过。原因很简单:证书的含金量不是单 ...
2026-11-02在校大专生能考的证书,大体分三类:通用能力类(英语等级、计算机等级、普通话水平测试)、职业技能等级类,以及部分不要求工作 ...
2026-11-02在家闲着想考个证书,最容易踩的坑是先去搜“含金量排行榜”,然后被一堆培训广告牵着走。更稳妥的顺序是反过来的:先看自己属于 ...
2026-11-02“注册类证书”不是一个可以随手列全的清单,它更像一个制度口径:通常指国家职业资格目录中设有注册管理、与执业准入挂钩的资格 ...
2026-11-02“现在考什么证有用”很难有统一答案。同一张证书,对想进会计事务所的人有用,对想做新媒体运营的人可能帮助有限;对需要评职称 ...
2026-11-02“适合女生考的证书有哪些含金量高”,这个问题里藏着两个容易走偏的前提。一是把“女生”当成职业属性,仿佛有一份统一的女性证 ...
2026-11-02先说结论:工商管理专业女生不必追“女生必考”榜单。证书有没有用,取决于它能不能对上你准备投的岗位。比较稳妥的做法,是先在 ...
2026-11-02“什么证好考又实用”这个问题,最怕两种回答:一种是列一堆“通过率高”的证书,却不告诉你考完能干什么;另一种是反过来只谈含 ...
2026-11-02“女士可以考哪些证书”并没有一份统一答案。真正决定你能考什么的,不是性别,而是三件事:你现在的身份(在校、在职还是准备转 ...
2026-11-02大专学历想考运营岗位证书,先给结论:能报的证书不少,但值得考的集中在三类——平台官方运营/投放认证、职业技能等级评价、数 ...
2026-11-02大学生判断一张证书是否重要,不用先看宣传热度,而要看四件事:它是否匹配你的目标岗位,校招时是否真用得上,在校期间是否具备 ...
2026-11-02先把结论说清楚:在校期间以2—4张核心证书为宜,其中1—2张通用底证、1—2张与目标岗位对应的方向证;高壁垒资格证只在符合报考 ...
2026-11-02大学生问证书推荐,最容易掉进的坑是“别人考什么我也考什么”。证书本身没有统一的含金量排序,同一张证,对目标岗位是硬门槛, ...
2026-11-02经管毕业生没有一张“通吃”的证书。比较有用的选择,是先按财会、金融、市场、人力、综合管理几个方向锁定目标岗位,再把证书分 ...
2026-11-02“程序员考什么证书含金量高”,没有放之四海皆准的答案。同样是“高含金量”,对后端开发、云原生工程师、数据开发和安全工程师 ...
2026-11-02