Python正则表达式使用经典实例


Posted in Python onJune 21, 2016

下面列出Python正则表达式的几种匹配用法,具体内容如下所示:

此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm

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)
Python 相关文章推荐
Python SQLAlchemy基本操作和常用技巧(包含大量实例,非常好)
May 06 Python
Python中列表和元组的使用方法和区别详解
Dec 30 Python
通过源码分析Python中的切片赋值
May 08 Python
PyQt 线程类 QThread使用详解
Jul 16 Python
Python二进制串转换为通用字符串的方法
Jul 23 Python
解决pandas .to_excel不覆盖已有sheet的问题
Dec 10 Python
Python图像处理库PIL的ImageDraw模块介绍详解
Feb 26 Python
pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
Apr 24 Python
查看keras各种网络结构各层的名字方式
Jun 11 Python
关于python的缩进规则的知识点详解
Jun 22 Python
在Python中实现字典反转案例
Dec 05 Python
Python Selenium操作Cookie的实例方法
Feb 28 Python
常见的python正则用法实例讲解
Jun 21 #Python
实例讲解Python的函数闭包使用中应注意的问题
Jun 20 #Python
Python中的数学运算操作符使用进阶
Jun 20 #Python
Python中在for循环中嵌套使用if和else语句的技巧
Jun 20 #Python
解析Python中的生成器及其与迭代器的差异
Jun 20 #Python
Python判断列表是否已排序的各种方法及其性能分析
Jun 20 #Python
Python编程中装饰器的使用示例解析
Jun 20 #Python
You might like
Discuz!下Memcache缓存实现方法
2010/05/28 PHP
php开发文档 会员收费1期
2012/08/14 PHP
php检测文本的编码
2015/07/26 PHP
PHP中类属性与类静态变量的访问方法示例
2016/07/13 PHP
php代码检查代理ip的有效性
2016/08/19 PHP
php文件操作之文件写入字符串、数组的方法分析
2019/04/15 PHP
js类型检查实现代码
2010/10/29 Javascript
Javascript单元测试框架QUnitjs详细介绍
2014/05/08 Javascript
JS简单实现String转Date的方法
2016/03/02 Javascript
AngularJS控制器详解及示例代码
2016/08/16 Javascript
AngularJS表单详解及示例代码
2016/08/17 Javascript
JavaScript制作颜色反转小游戏
2016/09/25 Javascript
JavaScript奇技淫巧44招【实用】
2016/12/11 Javascript
VUE axios上传图片到七牛的实例代码
2017/07/28 Javascript
JS组件系列之Gojs组件 前端图形化插件之利器
2017/11/29 Javascript
Vue点击切换颜色的方法
2018/09/13 Javascript
JQuery中queue方法用法示例
2019/01/31 jQuery
vue cli3.0结合echarts3.0与地图的使用方法示例
2019/03/26 Javascript
JS实现贪吃蛇游戏
2019/11/15 Javascript
Python实现的监测服务器硬盘使用率脚本分享
2014/11/07 Python
Python常用时间操作总结【取得当前时间、时间函数、应用等】
2017/05/11 Python
在Python 的线程中运行协程的方法
2020/02/24 Python
python GUI库图形界面开发之PyQt5结合Qt Designer创建信号与槽的详细方法与实例
2020/03/08 Python
Django返回HTML文件的实现方法
2020/09/17 Python
python爬虫scrapy图书分类实例讲解
2020/11/23 Python
英国网上电器商店:Electricshop
2020/03/15 全球购物
澳大利亚美容产品及化妆品在线:Activeskin
2020/06/03 全球购物
应届毕业生个人自我评价
2013/09/20 职场文书
进修护士自我鉴定
2013/10/14 职场文书
简单的大学生自我鉴定
2014/02/18 职场文书
总经理聘用协议书
2015/09/21 职场文书
学会掌握自己命运的十条黄金法则:
2019/08/08 职场文书
餐厅营销的秘密:为什么老顾客会流水?
2019/08/08 职场文书
营销策划分析:怎么策划才能更好销量产品?
2019/09/04 职场文书
python编写函数注意事项总结
2021/03/29 Python
MySQL性能指标TPS+QPS+IOPS压测
2022/08/05 MySQL