常见的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按行读取文件的简单实现方法
Jun 22 Python
Python如何判断数独是否合法
Sep 08 Python
python使用KNN算法手写体识别
Feb 01 Python
基于python 爬虫爬到含空格的url的处理方法
May 11 Python
3个用于数据科学的顶级Python库
Sep 29 Python
python 中pyqt5 树节点点击实现多窗口切换问题
Jul 04 Python
Python计算一个点到所有点的欧式距离实现方法
Jul 04 Python
python采集百度搜索结果带有特定URL的链接代码实例
Aug 30 Python
Python3 selenium 实现QQ群接龙自动化功能
Apr 17 Python
python 实现单例模式的5种方法
Sep 23 Python
Python基本数据类型之字符串str
Jul 21 Python
python读取mat文件生成h5文件的实现
Jul 15 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
source.php查看源文件
2006/12/09 PHP
php daodb插入、更新与删除数据
2009/03/19 PHP
PHP中基于ts与nts版本- vc6和vc9编译版本的区别详解
2013/04/26 PHP
PHP中判断变量为空的几种方法小结
2013/11/12 PHP
PHP上传图片进行等比缩放可增加水印功能
2014/01/13 PHP
ThinkPHP实现非标准名称数据表快速创建模型的方法
2014/11/29 PHP
在Win2003(64位)中配置IIS6+PHP5.2.17+MySQL5.5的运行环境
2016/04/04 PHP
php实现文件管理与基础功能操作
2017/03/21 PHP
PHP实现的ID混淆算法类与用法示例
2018/08/10 PHP
thinkphp5.1 框架导入/导出excel文件操作示例
2020/05/25 PHP
JavaScript 工具库 Cloudgamer JavaScript Library v0.1 发布
2009/10/29 Javascript
javascript 设计模式之单体模式 面向对象学习基础
2010/04/18 Javascript
JavaScript取得鼠标绝对位置程序代码介绍
2012/09/16 Javascript
JS.getTextContent(element,preformatted)使用介绍
2013/09/21 Javascript
通过Jquery的Ajax方法读取将table转换为Json
2014/05/31 Javascript
JavaScript实现重置表单(reset)的方法
2015/04/02 Javascript
基于javascript实现表格的简单操作
2016/05/21 Javascript
老生常谈js中的MVC
2017/07/25 Javascript
使用Bootstrap4 + Vue2实现分页查询的示例代码
2017/12/21 Javascript
vue mint-ui tabbar变组件使用
2018/05/04 Javascript
python根据距离和时长计算配速示例
2014/02/16 Python
PyTorch的深度学习入门教程之构建神经网络
2019/06/27 Python
django中嵌套的try-except实例
2020/05/21 Python
Python使用eval函数执行动态标表达式过程详解
2020/10/17 Python
手工制作的意大利太阳镜和光学元件:Illesteva
2019/01/19 全球购物
软件测试工程师结构化面试题库
2016/11/23 面试题
上学迟到的检讨书
2014/01/11 职场文书
尽职尽责村干部自我鉴定
2014/01/23 职场文书
《毛主席在花山》教学反思
2014/04/20 职场文书
住宿生擅自离校检讨书
2014/09/22 职场文书
十一国庆节“向国旗敬礼”主题班会活动方案
2014/09/27 职场文书
2015年教师党员公开承诺书
2015/01/22 职场文书
2015年端午节国旗下演讲稿
2015/03/19 职场文书
升职自我推荐信范文
2015/03/25 职场文书
2015年汽车销售员工作总结
2015/07/24 职场文书
创业计划书之情侣餐厅
2019/09/29 职场文书