京公网安备 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-03-06在数据分析、数据预处理场景中,dat文件是一种常见的二进制或文本格式数据文件,广泛应用于科研数据、工程数据、传感器数据等领 ...
2026-03-06在数据驱动决策的时代,CDA(Certified Data Analyst)数据分析师的核心价值,早已超越单纯的数据清洗与统计分析,而是通过数据 ...
2026-03-06在教学管理、培训数据统计、课程体系搭建等场景中,经常需要对课时数据进行排序并实现累加计算——比如,按课程章节排序,累加各 ...
2026-03-05在数据分析场景中,环比是衡量数据短期波动的核心指标——它通过对比“当前周期与上一个相邻周期”的数据,直观反映指标的月度、 ...
2026-03-05数据治理是数字化时代企业实现数据价值最大化的核心前提,而CDA(Certified Data Analyst)数据分析师作为数据全生命周期的核心 ...
2026-03-05在实验检测、质量控制、科研验证等场景中,“方法验证”是确保检测/分析结果可靠、可复用的核心环节——无论是新开发的检测方法 ...
2026-03-04在数据分析、科研实验、办公统计等场景中,我们常常需要对比两组数据的整体差异——比如两种营销策略的销售额差异、两种实验方案 ...
2026-03-04在数字化转型进入深水区的今天,企业对数据的依赖程度日益加深,而数据治理体系则是企业实现数据规范化、高质量化、价值化的核心 ...
2026-03-04在深度学习,尤其是卷积神经网络(CNN)的实操中,转置卷积(Transposed Convolution)是一个高频应用的操作——它核心用于实现 ...
2026-03-03在日常办公、数据分析、金融理财、科研统计等场景中,我们经常需要计算“平均值”来概括一组数据的整体水平——比如计算月度平均 ...
2026-03-03在数字化转型的浪潮中,数据已成为企业最核心的战略资产,而数据治理则是激活这份资产价值的前提——没有规范、高质量的数据治理 ...
2026-03-03在Excel办公中,数据透视表是汇总、分析繁杂数据的核心工具,我们常常通过它快速得到销售额汇总、人员统计、业绩分析等关键结果 ...
2026-03-02在日常办公和数据分析中,我们常常需要探究两个或多个数据之间的关联关系——比如销售额与广告投入是否正相关、员工出勤率与绩效 ...
2026-03-02在数字化运营中,时间序列数据是CDA(Certified Data Analyst)数据分析师最常接触的数据类型之一——每日的营收、每小时的用户 ...
2026-03-02在日常办公中,数据透视表是Excel、WPS等表格工具中最常用的数据分析利器——它能快速汇总繁杂数据、挖掘数据关联、生成直观报表 ...
2026-02-28有限元法(Finite Element Method, FEM)作为工程数值模拟的核心工具,已广泛应用于机械制造、航空航天、土木工程、生物医学等多 ...
2026-02-28在数字化时代,“以用户为中心”已成为企业运营的核心逻辑,而用户画像则是企业读懂用户、精准服务用户的关键载体。CDA(Certifi ...
2026-02-28在Python面向对象编程(OOP)中,类方法是构建模块化、可复用代码的核心载体,也是实现封装、继承、多态特性的关键工具。无论是 ...
2026-02-27在MySQL数据库优化中,索引是提升查询效率的核心手段—— 面对千万级、亿级数据量,合理创建索引能将查询时间从秒级压缩到毫秒级 ...
2026-02-27