常见的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使用MySQLdb访问mysql数据库的方法
Aug 03 Python
详解Python在七牛云平台的应用(一)
Dec 05 Python
Python数据可视化编程通过Matplotlib创建散点图代码示例
Dec 09 Python
Python实现可设置持续运行时间、线程数及时间间隔的多线程异步post请求功能
Jan 11 Python
Windows下的Jupyter Notebook 安装与自定义启动(图文详解)
Feb 21 Python
python通过zabbix api获取主机
Sep 17 Python
python使用xlsxwriter实现有向无环图到Excel的转换
Dec 12 Python
Django后台admin的使用详解
Jul 08 Python
python pptx复制指定页的ppt教程
Feb 14 Python
Python编程中Python与GIL互斥锁关系作用分析
Sep 15 Python
yolov5返回坐标的方法实例
Mar 17 Python
4种方法python批量修改替换列表中元素
Apr 07 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
dedecms 批量提取第一张图片最为缩略图的代码(文章+软件)
2009/10/29 PHP
Zend Framework教程之Zend_Layout布局助手详解
2016/03/04 PHP
PHP云打印类完整示例
2016/10/15 PHP
PHP goto语句用法实例
2019/08/06 PHP
php设计模式之职责链模式实例分析【星际争霸游戏案例】
2020/03/27 PHP
jQuery 各种浏览器下获得日期区别
2008/12/22 Javascript
javascript cookies操作集合
2010/04/12 Javascript
JQUERY的属性选择符和自定义选择符使用方法(二)
2011/04/07 Javascript
jQuery+CSS 实现随滚动条增减的汽水瓶中的液体效果
2011/09/26 Javascript
ejs v9 javascript模板系统
2012/03/21 Javascript
iframe子页面获取父页面元素的方法
2013/11/05 Javascript
jQuery读取XML文件内容的方法
2015/03/09 Javascript
jQuery中dom元素上绑定的事件详解
2015/04/24 Javascript
利用JavaScript的AngularJS库制作电子名片的方法
2015/06/18 Javascript
JQuery中解决重复动画的方法
2016/10/17 Javascript
详解Javascript百度地图接口开发文档中的类和方法
2017/02/07 Javascript
JS高仿抛物线加入购物车特效实现代码
2017/02/20 Javascript
Vue多种方法实现表头和首列固定的示例代码
2018/02/02 Javascript
Node.js上传文件功能之服务端如何获取文件上传进度
2018/02/05 Javascript
详解微信小程序input标签正则初体验
2018/08/18 Javascript
VUEX-action可以修改state吗
2019/11/19 Javascript
Python实现进程同步和通信的方法
2018/01/02 Python
python创造虚拟环境方法总结
2019/03/04 Python
详解Python图像处理库Pillow常用使用方法
2019/09/02 Python
Python3实现mysql连接和数据框的形成(实例代码)
2020/01/17 Python
神经网络训练采用gpu设置的方式
2021/03/03 Python
HTML5实现WebSocket协议原理浅析
2014/07/07 HTML / CSS
马来西亚在线药房:RoyalePharma
2019/12/01 全球购物
高中生家长会演讲稿
2014/01/14 职场文书
谢师宴邀请函
2015/02/02 职场文书
农村婚庆主持词
2015/06/29 职场文书
办公室规章制度范本
2015/08/04 职场文书
nginx如何将http访问的网站改成https访问
2021/03/31 Servers
python3使用diagrams绘制架构图的步骤
2021/04/08 Python
gateway网关接口请求的校验方式
2021/07/15 Java/Android
Spring中bean集合注入的方法详解
2022/07/07 Java/Android