常见的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操作CouchDB数据库简单示例
Mar 10 Python
python使用in操作符时元组和数组的区别分析
May 19 Python
Python实现根据IP地址和子网掩码算出网段的方法
Jul 30 Python
Python使用爬虫猜密码
Feb 19 Python
详解Python中的变量及其命名和打印
Mar 11 Python
python数据结构之列表和元组的详解
Sep 23 Python
安装python时MySQLdb报错的问题描述及解决方法
Mar 20 Python
Python中%是什么意思?python中百分号如何使用?
Mar 20 Python
使用python绘制3维正态分布图的方法
Dec 29 Python
tensorflow 重置/清除计算图的实现
Jan 19 Python
Python中使用aiohttp模拟服务器出现错误问题及解决方法
Oct 31 Python
用Python实现一个打字速度测试工具来测试你的手速
May 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
php数组函数序列之prev() - 移动数组内部指针到上一个元素的位置,并返回该元素值
2011/10/31 PHP
自己写的兼容低于PHP 5.5版本的array_column()函数
2014/10/24 PHP
详解Yii2 rules 的验证规则
2016/12/02 PHP
Mootools 图片展示插件(lightbox,ImageMenu)收集集合
2010/05/21 Javascript
非常漂亮的JS+CSS图片幻灯切换特效
2013/11/20 Javascript
Node.js中使用计时器定时执行函数详解
2014/08/15 Javascript
详解Matlab中 sort 函数用法
2016/03/20 Javascript
用JS生成UUID的方法实例
2016/03/30 Javascript
JS简单测试循环运行时间的方法
2016/09/04 Javascript
Bootstrap Table的使用总结
2016/10/08 Javascript
关于angularJs指令的Scope(作用域)介绍
2016/10/25 Javascript
Jquery EasyUI Datagrid右键菜单实现方法
2016/12/30 Javascript
深入理解JavaScript中的预解析
2017/01/04 Javascript
JavaScript中localStorage对象存储方式实例分析
2017/01/12 Javascript
老生常谈js中0到底是 true 还是 false
2017/03/08 Javascript
JavaScript实现小球沿正弦曲线运动
2020/09/07 Javascript
深入解析koa之中间件流程控制
2019/06/17 Javascript
vue中监听路由参数的变化及方法
2019/12/06 Javascript
Vue列表循环从指定下标开始的多种解决方案
2020/04/08 Javascript
微信小程序实现导航栏和内容上下联动功能代码
2020/06/29 Javascript
简单了解Vue computed属性及watch区别
2020/07/10 Javascript
[50:58]2018DOTA2亚洲邀请赛3月29日 小组赛A组OpTic VS Newbee
2018/03/30 DOTA
Python中循环引用(import)失败的解决方法
2018/04/22 Python
Ubuntu下Python2与Python3的共存问题
2018/10/31 Python
python对矩阵进行转置的2种处理方法
2019/07/17 Python
PyCharm使用Docker镜像搭建Python开发环境
2019/12/26 Python
Python的赋值、深拷贝与浅拷贝的区别详解
2020/02/12 Python
Python使用jupyter notebook查看ipynb文件过程解析
2020/06/02 Python
pycharm 使用anaconda为默认环境的操作
2021/02/05 Python
详解HTML5将footer置于页面最底部的方法(CSS+JS)
2018/10/11 HTML / CSS
美国最大的船只买卖在线市场:Boat Trader
2018/08/04 全球购物
写给学生的新学期寄语
2014/01/18 职场文书
古汉语文学求职信范文
2014/03/16 职场文书
先进个人申报材料
2014/12/30 职场文书
2016幼儿园新学期寄语
2015/12/03 职场文书
解析原生JS getComputedStyle
2021/05/25 Javascript