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
以下的文章内容来源于柯家媛老师的专栏,如果您想阅读专栏《小白必备的数据思维课》,点击下方链接 https://edu.cda.cn/goods/sh ...
2025-03-11随着数字化转型的加速,企业积累了海量数据,如何从这些数据中挖掘有价值的信息,成为企业提升竞争力的关键。CDA认证考试体系应 ...
2025-03-10推荐学习书籍 《CDA一级教材》在线电子版正式上线CDA网校,为你提供系统、实用、前沿的学习资源,助你轻松迈入数据分析的大门! ...
2025-03-07在数据驱动决策的时代,掌握多样的数据分析方法,就如同拥有了开启宝藏的多把钥匙,能帮助我们从海量数据中挖掘出关键信息,本 ...
2025-03-06在备考 CDA 考试的漫漫征途上,拥有一套契合考试大纲的优质模拟题库,其重要性不言而喻。它恰似黑夜里熠熠生辉的启明星,为每一 ...
2025-03-05“纲举目张,执本末从。”若想在数据分析领域有所收获,一套合适的学习教材至关重要。一套优质且契合需求的学习教材无疑是那关 ...
2025-03-04以下的文章内容来源于刘静老师的专栏,如果您想阅读专栏《10大业务分析模型突破业务瓶颈》,点击下方链接 https://edu.cda.cn/go ...
2025-03-04在现代商业环境中,数据分析师的角色愈发重要。数据分析师通过解读数据,帮助企业做出更明智的决策。因此,考取数据分析师证书成为了许多人提升职业竞争力的选择。本文将详细介绍考取数据分析师证书的过程,包括了解证书种类和 ...
2025-03-03在当今信息化社会,大数据已成为各行各业不可或缺的宝贵资源。大数据专业应运而生,旨在培养具备扎实理论基础和实践能力,能够应 ...
2025-03-03数据分析师认证考试全面升级后,除了考试场次和报名时间,小伙伴们最关心的就是报名费了,报 ...
2025-03-032025年刚开启,知乎上就出现了一个热帖: 2024年突然出现的经济下行,使各行各业都感觉到压力山大。有人说,大环境越来越不好了 ...
2025-03-03大数据分析师培训旨在培养学员掌握大数据分析的基础知识、技术及应用能力,以适应企业对数据分析人才的需求。根据不同的培训需求 ...
2025-03-03小伙伴们,最近被《哪吒2》刷屏了吧!这部电影不仅在国内掀起观影热潮,还在全球范围内引发了关注,成为中国电影崛起的又一里程 ...
2025-03-03以下的文章内容来源于张彦存老师的专栏,如果您想阅读专栏《Python 数据可视化 18 讲(PyEcharts、Matplotlib、Seaborn)》,点 ...
2025-02-28最近,国产AI模型DeepSeek爆火,其创始人梁文峰走进大众视野。《黑神话:悟空》制作人冯骥盛赞DeepSeek为“国运级别的科技成果” ...
2025-02-271.统计学简介 听说你已经被统计学劝退,被Python唬住……先别着急划走,看完这篇再说! 先说结论,大多数情况下的学不会都不是知 ...
2025-02-27“我们的利润率上升了,但销售额却没变,这是为什么?” “某个业务的市场份额在下滑,到底是什么原因?” “公司整体业绩稳定, ...
2025-02-26在数据分析工作中,你可能经常遇到这样的问题: 从浏览到消费的转化率一直很低,那到底该优化哪里呢? 如果你要投放广告该怎么 ...
2025-02-25近来deepseek爆火,看看deepseek能否帮我们快速实现数据看板实时更新。 可以看出这对不知道怎么动手的小白来说是相当友好的,尤 ...
2025-02-25挖掘用户价值本质是让企业从‘赚今天的钱’升级为‘赚未来的钱’,同时让用户从‘被推销’变为‘被满足’。询问deepseek关于挖 ...
2025-02-25