常见的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 相关文章推荐
vc6编写python扩展的方法分享
Jan 17 Python
python中函数传参详解
Jul 03 Python
python使用json序列化datetime类型实例解析
Feb 11 Python
分析Python读取文件时的路径问题
Feb 11 Python
python 实时得到cpu和内存的使用情况方法
Jun 11 Python
python使用numpy读取、保存txt数据的实例
Oct 14 Python
python求绝对值的三种方法小结
Dec 04 Python
python关于调用函数外的变量实例
Dec 26 Python
django中url映射规则和服务端响应顺序的实现
Apr 02 Python
django在开发中取消外键约束的实现
May 20 Python
python中Django文件上传方法详解
Aug 05 Python
PyQt5结合QtDesigner实现文本框读写操作
Jun 11 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
Linux编译升级php的详细方法
2013/11/04 PHP
php生成Android客户端扫描可登录的二维码
2016/05/13 PHP
Yii框架用户登录session丢失问题解决方法
2017/01/07 PHP
PHP数组Key强制类型转换实现原理解析
2020/09/01 PHP
JQuery实现简单时尚快捷的气泡提示插件
2012/12/20 Javascript
js 针对html DOM元素操作等经验累积
2014/03/11 Javascript
Javascript学习笔记之 对象篇(四) : for in 循环
2014/06/24 Javascript
Bootstrap表单Form全面解析
2016/06/13 Javascript
通过JS获取Request.QueryString()参数的值实现方法
2016/09/27 Javascript
jQuery设置和获取select、checkbox、radio的选中值方法
2017/01/01 Javascript
bootstrap选项卡使用方法解析
2017/01/11 Javascript
JS实现的二叉树算法完整实例
2017/04/06 Javascript
mui back 返回刷新页面的实例
2017/12/06 Javascript
基于vue 添加axios组件,解决post传参数为null的问题
2018/03/05 Javascript
详解使用jQuery.i18n.properties实现js国际化
2018/05/04 jQuery
如何将HTML字符转换为DOM节点并动态添加到文档中详解
2018/08/19 Javascript
jQuery实现的自定义轮播图功能详解
2018/12/28 jQuery
解决vant中 tab栏遇到的坑 van-tabs
2020/11/04 Javascript
[01:24]DOTA2上海特锦赛OG战队抵达 专车接机入驻总统套房
2016/02/23 DOTA
python fabric实现远程部署
2017/01/05 Python
使用tensorflow实现AlexNet
2017/11/20 Python
500行Python代码打造刷脸考勤系统
2019/06/03 Python
python3.6连接mysql数据库及增删改查操作详解
2020/02/10 Python
ubuntu 安装pyqt5和卸载pyQt5的方法
2020/03/24 Python
Django-migrate报错问题解决方案
2020/04/21 Python
Python 通过正则表达式快速获取电影的下载地址
2020/08/17 Python
python删除文件、清空目录的实现方法
2020/09/23 Python
英国第一蛋白粉品牌:Myprotein
2016/09/14 全球购物
台湾专柜女包:KINAZ
2019/12/26 全球购物
sort命令的作用和用法
2013/08/25 面试题
剪枝的学问教学反思
2014/02/07 职场文书
研发工程师岗位职责
2014/04/28 职场文书
家庭贫困证明
2014/09/23 职场文书
党政领导班子群众路线对照检查材料
2014/10/26 职场文书
2014年学校禁毒工作总结
2014/12/23 职场文书
使用 CSS 轻松实现一些高频出现的奇形怪状按钮
2021/12/06 HTML / CSS