Python中正则表达式的用法实例汇总


Posted in Python onAugust 18, 2014

正则表达式是Python程序设计中非常实用的功能,本文就常用的正则表达式做一汇总,供大家参考之用。具体如下:

一、字符串替换

1.替换所有匹配的子串

用newstring替换subject中所有与正则表达式regex匹配的子串

result, number = re.subn(regex, newstring, subject)

2.替换所有匹配的子串(使用正则表达式对象)

reobj = re.compile(regex)
result, number = reobj.subn(newstring, subject)

二、字符串拆分

1.字符串拆分

result = re.split(regex, subject)

2.字符串拆分(使用正则表示式对象)

reobj = re.compile(regex)
result = reobj.split(subject)

三、匹配

下面列出Python正则表达式的几种匹配用法:

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. 创建一个匹配对象,然后通过该对象获得匹配细节

regex=ur"..." #正则表达式
match = re.search(regex, subject)
if match:
  # match start: match.start()
  # match end (exclusive): match.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): match.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): match.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()

感兴趣的读者可以动手调试一下本文实例代码,相信会有新的收获。

Python 相关文章推荐
如何利用Fabric自动化你的任务
Oct 20 Python
python操作 hbase 数据的方法
Dec 18 Python
python reduce 函数使用详解
Dec 05 Python
神经网络理论基础及Python实现详解
Dec 15 Python
python3+PyQt5实现文档打印功能
Apr 24 Python
linux查找当前python解释器的位置方法
Feb 20 Python
python实现静态服务器
Sep 05 Python
Python利用Scrapy框架爬取豆瓣电影示例
Jan 17 Python
Tensorflow限制CPU个数实例
Feb 06 Python
TensorFlow2.X结合OpenCV 实现手势识别功能
Apr 08 Python
Python装饰器如何实现修复过程解析
Sep 05 Python
OpenCV图像变换之傅里叶变换的一些应用
Jul 26 Python
python中enumerate的用法实例解析
Aug 18 #Python
Python采用raw_input读取输入值的方法
Aug 18 #Python
Python中Collection的使用小技巧
Aug 18 #Python
Python实现3行代码解简单的一元一次方程
Aug 18 #Python
Python统计列表中的重复项出现的次数的方法
Aug 18 #Python
Python中无限元素列表的实现方法
Aug 18 #Python
Python中实现字符串类型与字典类型相互转换的方法
Aug 18 #Python
You might like
zf框架的数据库追踪器使用示例
2014/03/13 PHP
PHP之uniqid()函数用法
2014/11/03 PHP
php带抄送和密件抄送的邮件发送方法
2015/03/20 PHP
PHP常用设计模式之委托设计模式
2016/02/13 PHP
PHP自定义函数实现格式化秒的方法
2016/09/14 PHP
php表单文件iframe异步上传实例讲解
2017/07/26 PHP
Laravel路由研究之domain解决多域名问题的方法示例
2019/04/04 PHP
用 Javascript 验证表单(form)中多选框(checkbox)值
2009/09/08 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(二)人物行走的实现
2013/01/23 Javascript
JS 获取浏览器和屏幕宽高等信息的实现思路及代码
2013/07/31 Javascript
使用jquery.upload.js实现异步上传示例代码
2014/07/29 Javascript
Nodejs实现多人同时在线移动鼠标的小游戏分享
2014/12/06 NodeJs
JavaScript实现在标题栏上显示当前日期的方法
2015/03/19 Javascript
JavaScript组件开发完整示例
2015/12/15 Javascript
使用JS中的Replace()方法遇到的问题小结
2017/10/20 Javascript
vue中的watch监听数据变化及watch中各属性的详解
2018/09/11 Javascript
JS实现简单的文字无缝上下滚动功能示例
2019/06/22 Javascript
微信小程序中data-key属性之数据传输(经验总结)
2020/08/22 Javascript
Vue中nprogress页面加载进度条的方法实现
2020/11/13 Javascript
python调用cmd命令行制作刷博器
2014/01/13 Python
Python中的fileinput模块的简单实用示例
2015/07/09 Python
Python实现多线程HTTP下载器示例
2017/02/11 Python
python控制台实现tab补全和清屏的例子
2019/08/20 Python
Python生成验证码、计算具体日期是一年中的第几天实例代码详解
2019/10/16 Python
Python3 Click模块的使用方法详解
2020/02/12 Python
Java多线程实现四种方式原理详解
2020/06/02 Python
完美解决keras保存好的model不能成功加载问题
2020/06/11 Python
纯CSS3实现8组超炫酷鼠标滑过图片动画
2016/03/16 HTML / CSS
美国办公用品购物网站:Quill.com
2016/09/01 全球购物
孕妇内衣和胸罩:Cake Maternity
2018/07/16 全球购物
Cynthia Rowley官网:全球领先的生活方式品牌
2020/10/27 全球购物
搬家公司的创业计划书
2014/01/01 职场文书
高中同学聚会邀请函
2014/01/11 职场文书
拖鞋店创业计划书
2014/01/15 职场文书
2015年语言文字工作总结
2015/07/23 职场文书
解决Goland 同一个package中函数互相调用的问题
2021/05/06 Golang