常见的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 相关文章推荐
Django中实现一个高性能计数器(Counter)实例
Jul 09 Python
详细解读Python中的__init__()方法
May 02 Python
深入理解Python中的*重复运算符
Oct 28 Python
python实现简单淘宝秒杀功能
May 03 Python
pycharm在调试python时执行其他语句的方法
Nov 29 Python
简单了解python 邮件模块的使用方法
Jul 24 Python
详解pyinstaller selenium python3 chrome打包问题
Oct 18 Python
Python 静态方法和类方法实例分析
Nov 21 Python
Python实现线性判别分析(LDA)的MATLAB方式
Dec 09 Python
解决Keras自带数据集与预训练model下载太慢问题
Jun 12 Python
如何使用Cython对python代码进行加密
Jul 08 Python
python实现简单的学生管理系统
Feb 22 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 Web开发MVC框架的Smarty使用说明
2013/04/19 PHP
解析yii数据库的增删查改
2013/06/20 PHP
php上传文件中文文件名乱码的解决方法
2013/11/01 PHP
PHP实现格式化文件数据大小显示的方法
2015/01/03 PHP
CodeIgniter框架验证码类库文件与用法示例
2017/03/18 PHP
Yii框架日志操作图文与实例详解
2019/09/09 PHP
十分钟打造AutoComplete自动完成效果代码
2009/12/26 Javascript
IE图片缓存document.execCommand(&quot;BackgroundImageCache&quot;,false,true)
2011/03/01 Javascript
js修改table中Td的值(定义td的单击事件)
2013/01/10 Javascript
js获取html参数及向swf传递参数应用介绍
2013/02/18 Javascript
js 针对html DOM元素操作等经验累积
2014/03/11 Javascript
a标签的href与onclick事件的区别详解
2014/11/12 Javascript
JavaScript Math.ceil 方法(对数值向上取整)
2015/01/09 Javascript
jQuery实现仿路边灯箱广告图片轮播效果
2015/04/15 Javascript
Avalon中文长字符截取、关键字符隐藏、自定义过滤器
2016/05/18 Javascript
JavaScript prototype属性详解
2016/10/25 Javascript
javascript跨域请求包装函数与用法示例
2016/11/03 Javascript
vue引入ueditor及node后台配置详解
2018/01/03 Javascript
JavaScript实现的贝塞尔曲线算法简单示例
2018/01/30 Javascript
three.js实现圆柱体
2018/12/30 Javascript
Vue核心概念Action的总结
2019/01/18 Javascript
JS实现的字符串数组去重功能小结
2019/06/17 Javascript
小程序实现分类页
2019/07/12 Javascript
Vue+Element实现网页版个人简历系统(推荐)
2019/12/31 Javascript
Python实现栈的方法
2015/05/26 Python
Python教程之全局变量用法
2016/06/27 Python
python爬虫 使用真实浏览器打开网页的两种方法总结
2018/04/21 Python
python 文本单词提取和词频统计的实例
2018/12/22 Python
对python 合并 累加两个dict的实例详解
2019/01/21 Python
Python中实现输入一个整数的案例
2020/05/03 Python
利用Python发送邮件或发带附件的邮件
2020/11/12 Python
中学老师的自我评价
2013/11/07 职场文书
销售职业生涯规划范文
2014/03/14 职场文书
辛德勒的名单观后感
2015/06/03 职场文书
赡养老人协议书范本
2015/08/06 职场文书
Python3中最常用的5种线程锁实例总结
2021/07/07 Python