常见的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 相关文章推荐
Python2.x中str与unicode相关问题的解决方法
Mar 30 Python
Python冒泡排序注意要点实例详解
Sep 09 Python
Python脚本实现Web漏洞扫描工具
Oct 25 Python
Python比较2个时间大小的实现方法
Apr 10 Python
Python 实现网页自动截图的示例讲解
May 17 Python
python实现批量图片格式转换
Jun 16 Python
解决python3 Pycharm上连接数据库时报错的问题
Dec 03 Python
python自定义函数实现一个数的三次方计算方法
Jan 20 Python
python网络爬虫 Scrapy中selenium用法详解
Sep 28 Python
python虚拟环境模块venv使用及示例
Mar 04 Python
基于CentOS搭建Python Django环境过程解析
Aug 24 Python
OpenCV-Python实现轮廓的特征值
Jun 09 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
php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比
2015/04/14 PHP
TP5框架实现自定义分页样式的方法示例
2020/04/05 PHP
js获取div高度的代码
2008/08/09 Javascript
jQuery toggle()设置CSS样式
2009/11/05 Javascript
jquery监控数据是否变化(修正版)
2011/04/12 Javascript
防止文件缓存的js代码
2013/01/10 Javascript
javascript 获取网页标题代码实例
2014/01/22 Javascript
选择复选框按钮置灰否则按钮可用
2014/05/22 Javascript
angularJS中$apply()方法详解
2015/01/07 Javascript
jquery插件corner实现圆角边框的方法
2015/03/09 Javascript
JS实现的打字机效果完整实例
2016/06/20 Javascript
使用async、enterproxy控制并发数量的方法详解
2018/01/02 Javascript
webpack 4.0.0-beta.0版本新特性介绍
2018/02/10 Javascript
详解vue-cli@2.x项目迁移日志
2019/06/06 Javascript
Windows环境下python环境安装使用图文教程
2018/03/13 Python
Python基于最小二乘法实现曲线拟合示例
2018/06/14 Python
简单了解python元组tuple相关原理
2019/12/02 Python
Django中modelform组件实例用法总结
2020/02/10 Python
三步解决python PermissionError: [WinError 5]拒绝访问的情况
2020/04/22 Python
浅谈keras2 predict和fit_generator的坑
2020/06/17 Python
Python scrapy爬取小说代码案例详解
2020/07/09 Python
CSS3 实现发光边框特效
2020/11/11 HTML / CSS
丹尼尔惠灵顿手表天猫官方旗舰店:Daniel Wellington
2017/08/25 全球购物
乐高瑞士官方商店:LEGO CH
2020/08/16 全球购物
介绍一下.NET构架下remoting和webservice
2014/05/08 面试题
某同学的自我鉴定范文
2013/12/26 职场文书
生日派对邀请函
2014/01/13 职场文书
主题婚礼策划方案
2014/02/10 职场文书
行政专员求职信范文
2014/05/03 职场文书
商业街策划方案
2014/05/31 职场文书
2014年超市员工工作总结
2014/11/18 职场文书
大学学生会辞职信
2015/05/13 职场文书
退税申请报告怎么写
2015/05/18 职场文书
初中班主任心得体会
2016/01/07 职场文书
详解Android中的TimePickerView(时间选择器)的用法
2022/04/30 Java/Android
cypress测试本地web应用
2022/06/01 Javascript