常见的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()

18.字符串替换
 1).替换所有匹配的子串 

#用newstring替换subject中所有与正则表达式regex匹配的子串
result = re.sub(regex, newstring, subject)

2).替换所有匹配的子串(使用正则表达式对象) 

reobj = re.compile(regex)
 result = reobj.sub(newstring, subject)

19.字符串拆分
 1).字符串拆分 

result = re.split(regex, subject)

2).字符串拆分(使用正则表示式对象)
reobj = re.compile(regex)
 result = reobj.split(subject)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
分享一个常用的Python模拟登陆类
Mar 29 Python
python实现爬取千万淘宝商品的方法
Jun 30 Python
使用Python脚本生成随机IP的简单方法
Jul 30 Python
Python爬虫实例爬取网站搞笑段子
Nov 08 Python
Python如何获得百度统计API的数据并发送邮件示例代码
Jan 27 Python
Python实战之制作天气查询软件
May 14 Python
Python异步操作MySQL示例【使用aiomysql】
May 16 Python
在Pytorch中计算自己模型的FLOPs方式
Dec 30 Python
Ubuntu18.04安装 PyCharm并使用 Anaconda 管理的Python环境
Apr 08 Python
安装并免费使用Pycharm专业版(学生/教师)
Sep 24 Python
python 下划线的不同用法
Oct 24 Python
Sentry错误日志监控使用方法解析
Nov 12 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
12步入门Python中的decorator装饰器使用方法
Jun 20 #Python
You might like
当年上海收录机产品生产,进口和价格情况
2021/03/04 无线电
Thinkphp+smarty+uploadify实现无刷新上传
2015/07/30 PHP
php图片上传类 附调用方法
2016/05/15 PHP
国外的为初学者写的JavaScript教程
2008/06/09 Javascript
关于JavaScript定义类和对象的几种方式
2010/11/09 Javascript
基于jQuery实现的Ajax 验证用户名是否存在的实现代码
2011/04/06 Javascript
基于jquery的使ListNav兼容中文首字拼音排序的实现代码
2011/07/10 Javascript
js的2种继承方式详解
2014/03/04 Javascript
js结合正则实现国内手机号段校验
2015/06/19 Javascript
JS模仿腾讯图片站的图片翻页按钮效果完整实例
2016/06/21 Javascript
JS之获取样式的简单实现方法(推荐)
2016/09/13 Javascript
浅析如何利用JavaScript进行语音识别
2016/10/27 Javascript
js实现九宫格的随机颜色跳转
2017/02/19 Javascript
AngularJS基于factory创建自定义服务的方法详解
2017/05/25 Javascript
利用vscode编写vue的简单配置详解
2017/06/17 Javascript
基于Vuex无法观察到值变化的解决方法
2018/03/01 Javascript
利用node 判断打开的是文件 还是 文件夹的实例
2019/06/10 Javascript
JS实现简单的表格增删
2020/01/16 Javascript
Vue中keep-alive组件的深入理解
2020/08/23 Javascript
python循环监控远程端口的方法
2015/03/14 Python
python编码总结(编码类型、格式、转码)
2016/07/01 Python
Python将多个list合并为1个list的方法
2018/06/27 Python
对python3.4 字符串转16进制的实例详解
2019/06/12 Python
python 列表转为字典的两个小方法(小结)
2019/06/28 Python
Python 读取用户指令和格式化打印实现解析
2019/09/02 Python
Python文本处理简单易懂方法解析
2019/12/19 Python
python批量替换文件名中的共同字符实例
2020/03/05 Python
Ratchet 模态框的实现
2020/08/19 HTML / CSS
Waterford加拿大官方网站:世界著名的水晶杯品牌
2016/11/01 全球购物
中专生自我鉴定范文
2014/02/02 职场文书
《都江堰》教学反思
2014/02/07 职场文书
企业三严三实学习心得体会
2014/10/13 职场文书
毕业论文答辩开场白和结束语
2015/05/27 职场文书
信用卡工资证明范本
2015/06/19 职场文书
left join、inner join、right join的区别
2021/04/05 MySQL
浅谈spring boot使用thymeleaf版本的问题
2021/08/04 Java/Android