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 相关文章推荐
Windows下Eclipse+PyDev配置Python+PyQt4开发环境
May 17 Python
Python实现将罗马数字转换成普通阿拉伯数字的方法
Apr 19 Python
使用python装饰器计算函数运行时间的实例
Apr 21 Python
Python 使用类写装饰器的小技巧
Sep 30 Python
在Python中输入一个以空格为间隔的数组方法
Nov 13 Python
使用Python给头像戴上圣诞帽的图像操作过程解析
Sep 20 Python
Python Pandas对缺失值的处理方法
Sep 27 Python
Django后端发送小程序微信模板消息示例(服务通知)
Dec 17 Python
opencv 实现特定颜色线条提取与定位操作
Jun 02 Python
Python使用urlretrieve实现直接远程下载图片的示例代码
Aug 17 Python
python对 MySQL 数据库进行增删改查的脚本
Oct 22 Python
python的列表生成式,生成器和generator对象你了解吗
Mar 16 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
Terran兵种介绍
2020/03/14 星际争霸
关于页面优化和伪静态
2009/10/11 PHP
PHP在引号前面添加反斜杠(PHP去除反斜杠)
2013/09/28 PHP
PHP 导出Excel示例分享
2014/08/18 PHP
PHP中使用php://input处理相同name值的表单数据
2015/02/03 PHP
PHP中迭代器的简单实现及Yii框架中的迭代器实现方法示例
2020/04/26 PHP
CSS+JS构建的图片查看器
2006/07/22 Javascript
Javascript 函数对象的多重身份
2009/06/28 Javascript
jQuery技巧大放送 学习jquery的朋友可以看下
2009/10/14 Javascript
extjs实现选择多表自定义查询功能 前台部分(ext源码)
2011/12/20 Javascript
防止文件缓存的js代码
2013/01/10 Javascript
node.js中的buffer.slice方法使用说明
2014/12/10 Javascript
JavaScript中对象介绍
2014/12/31 Javascript
jQuery中not()方法用法实例
2015/01/06 Javascript
Javascript中3个需要注意的运算符
2015/04/02 Javascript
全面了解构造函数继承关键apply call
2016/07/26 Javascript
详解XMLHttpRequest(二)响应属性、二进制数据、监测上传下载进度
2016/09/14 Javascript
JS实现图片预览的两种方式
2017/06/27 Javascript
vue+mockjs模拟数据实现前后端分离开发的实例代码
2017/08/08 Javascript
Layer弹出层动态获取数据的方法
2018/08/20 Javascript
原生javascript的ajax请求及后台PHP响应操作示例
2020/02/24 Javascript
openlayers 3实现车辆轨迹回放
2020/09/24 Javascript
[05:17]DOTA2誓师:今天我们在这里 明天TI4等我!
2014/03/26 DOTA
Python定时执行之Timer用法示例
2015/05/27 Python
对python中Json与object转化的方法详解
2018/12/31 Python
pyQT5 实现窗体之间传值的示例
2019/06/20 Python
Python交互式图形编程的实现
2019/07/25 Python
Keras:Unet网络实现多类语义分割方式
2020/06/11 Python
写求职信有哪些注意事项
2014/05/08 职场文书
期末复习计划
2015/01/19 职场文书
会计出纳岗位职责
2015/03/31 职场文书
2015年中职班主任工作总结
2015/05/25 职场文书
会计工作自我鉴定范文
2019/06/21 职场文书
pytorch实现手写数字图片识别
2021/05/20 Python
Python matplotlib绘制雷达图
2022/04/13 Python
SQL Server数据库的三种创建方法汇总
2023/05/08 MySQL