常见的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字符串处理函数简明总结
Apr 13 Python
在Python中测试访问同一数据的竞争条件的方法
Apr 23 Python
在Python中使用mongoengine操作MongoDB教程
Apr 24 Python
pymongo给mongodb创建索引的简单实现方法
May 06 Python
Python下rrdtool模块的基本使用方法
Nov 13 Python
python文本数据相似度的度量
Mar 12 Python
在cmd中运行.py文件: python的操作步骤
May 12 Python
Django 使用Ajax进行前后台交互的示例讲解
May 28 Python
Python+OpenCV实现旋转文本校正方式
Jan 09 Python
解决reload(sys)后print失效的问题
Apr 25 Python
解决Keras 自定义层时遇到版本的问题
Jun 16 Python
Python之基础函数案例详解
Aug 30 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
附件名前加网站名
2008/03/23 PHP
PHP表单递交控件名称含有点号(.)会被转化为下划线(_)的处理方法
2013/01/06 PHP
PHP实现将base64编码字符串转换成图片示例
2018/06/22 PHP
php中加密解密DES类的简单使用方法示例
2020/03/26 PHP
JS类定义原型方法的两种实现的区别评论很多
2007/09/12 Javascript
javascript 表格排序和表头浮动效果(扩展SortTable)
2009/04/07 Javascript
页面只能打开一次Cooike如何实现
2012/12/04 Javascript
Javascript 遍历页面text控件详解
2014/01/06 Javascript
node.js中的fs.lstatSync方法使用说明
2014/12/16 Javascript
JavaScript获取浏览器信息的方法
2015/11/20 Javascript
分享15个大家都熟知的jquery小技巧
2015/12/02 Javascript
原生JS封装ajax 传json,str,excel文件上传提交表单(推荐)
2016/06/21 Javascript
Javascript 实现简单计算器实例代码
2016/10/23 Javascript
Chrome不支持showModalDialog模态对话框和无法返回returnValue问题的解决方法
2016/10/30 Javascript
JS插件plupload.js实现多图上传并显示进度条
2016/11/29 Javascript
vue中上传视频或图片或图片和文字一起到后端的解决方法
2019/12/01 Javascript
Vue 请求传公共参数的操作
2020/07/31 Javascript
[01:02:10]DOTA2上海特级锦标赛B组小组赛#2 VG VS Fnatic第一局
2016/02/26 DOTA
Django中利用filter与simple_tag为前端自定义函数的实现方法
2017/06/15 Python
python使用threading获取线程函数返回值的实现方法
2017/11/15 Python
python3连接MySQL数据库实例详解
2018/05/24 Python
基于Pandas读取csv文件Error的总结
2018/06/15 Python
idea创建springMVC框架和配置小文件的教程图解
2018/09/18 Python
使用virtualenv创建Python环境及PyQT5环境配置的方法
2019/09/10 Python
Python递归调用实现数字累加的代码
2020/02/25 Python
如何导出python安装的所有模块名称和版本号到文件中
2020/06/05 Python
Python爬虫之爬取淘女郎照片示例详解
2020/07/28 Python
Django基于Models定制Admin后台实现过程解析
2020/11/11 Python
Skyscanner波兰:廉价航班
2017/11/07 全球购物
用Python写一个for循环的例子
2016/07/19 面试题
医学生自我鉴定范文
2013/11/08 职场文书
詹天佑教学反思
2014/04/30 职场文书
2015年幼师工作总结
2015/04/28 职场文书
小学教师师德培训心得体会
2016/01/09 职场文书
通知怎么写?
2019/04/17 职场文书
什么是创业计划书?什么是商业计划书?这里一一解答
2019/07/12 职场文书