常见的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 20 Python
利用Python自动监控网站并发送邮件告警的方法
Aug 24 Python
python 实现一个贴吧图片爬虫的示例
Oct 12 Python
Python编程使用tkinter模块实现计算器软件完整代码示例
Nov 29 Python
python 获取当天每个准点时间戳的实例
May 22 Python
Tensorflow 实现修改张量特定元素的值方法
Jul 30 Python
Python之两种模式的生产者消费者模型详解
Oct 26 Python
Python 微信爬虫完整实例【单线程与多线程】
Jul 06 Python
tensorflow多维张量计算实例
Feb 11 Python
python实现横向拼接图片
Mar 23 Python
django的autoreload机制实现
Jun 03 Python
使用scrapy ImagesPipeline爬取图片资源的示例代码
Sep 28 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
异世界新番又来了,同样是从零开始,男主的年龄降到5岁
2020/04/09 日漫
php学习之 数组声明
2011/06/09 PHP
php中使用Imagick实现图像直方图的实现代码
2011/08/30 PHP
ThinkPHP上使用多说评论插件的方法
2014/10/31 PHP
PHP7.1实现的AES与RSA加密操作示例
2018/06/15 PHP
实例讲解PHP验证邮箱是否合格
2019/01/28 PHP
基于jQuery试卷自动排版系统
2010/07/18 Javascript
Jquery倒数计时按钮setTimeout的实例代码
2013/07/04 Javascript
JS 实现导航栏悬停效果(续)
2013/09/24 Javascript
谷歌浏览器不支持showModalDialog模态对话框的解决方法
2014/09/22 Javascript
JavaScript中的getMilliseconds()方法使用详解
2015/06/10 Javascript
jquery分页插件jquery.pagination.js实现无刷新分页
2016/04/01 Javascript
jQuery EasyUI中DataGird动态生成列的方法
2016/04/05 Javascript
Bootstrap+jfinal退出系统弹出确认框的实现方法
2016/05/30 Javascript
js实现碰撞检测特效代码分享
2016/10/16 Javascript
JS实现点击按钮可实现编辑功能
2018/07/03 Javascript
jQuery实现手风琴效果(蒙版)
2020/01/11 jQuery
详解JSON.stringify()的5个秘密特性
2020/05/26 Javascript
vue基础知识--axios合并请求和slot
2020/06/04 Javascript
Python单链表的简单实现方法
2014/09/23 Python
Python编程入门的一些基本知识
2015/05/13 Python
python使用in操作符时元组和数组的区别分析
2015/05/19 Python
Python SVM(支持向量机)实现方法完整示例
2018/06/19 Python
示例详解Python3 or Python2 两者之间的差异
2018/08/23 Python
pycharm 关掉syntax检查操作
2020/06/09 Python
浅谈Python __init__.py的作用
2020/10/28 Python
Html5实现用户注册自动校验功能实例代码
2016/05/24 HTML / CSS
html5 初试 indexedDB(推荐)
2016/07/21 HTML / CSS
Erwin Müller穆勒家居瑞士官网:您整个家庭的邮购公司
2019/12/28 全球购物
继承时候类的执行顺序问题,一般都是选择题,问你将会打印出什么?
2015/11/18 面试题
幼儿园家长会欢迎词
2014/01/09 职场文书
2014年上半年工作自我评价
2014/01/18 职场文书
名企HR怎样看待求职信
2014/02/23 职场文书
2014年药店工作总结
2014/11/20 职场文书
个人先进事迹材料
2014/12/29 职场文书
分享MySQL常用 内核 Debug 几种常见方法
2022/03/17 MySQL