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 相关文章推荐
Python中的异常处理简明介绍
Apr 13 Python
Python的Flask框架中实现简单的登录功能的教程
Apr 20 Python
Python实现求最大公约数及判断素数的方法
May 26 Python
Django视图和URL配置详解
Jan 31 Python
基于python代码实现简易滤除数字的方法
Jul 17 Python
在pycharm中python切换解释器失败的解决方法
Oct 29 Python
对python中的six.moves模块的下载函数urlretrieve详解
Dec 19 Python
基于YUV 数据格式详解及python实现方式
Dec 09 Python
Python开发之pip安装及使用方法详解
Feb 21 Python
Python绘制词云图之可视化神器pyecharts的方法
Feb 23 Python
python字典的元素访问实例详解
Jul 21 Python
Python如何利用pandas读取csv数据并绘图
Jul 07 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
一个简单的自动发送邮件系统(二)
2006/10/09 PHP
php面向对象全攻略 (八)重载新的方法
2009/09/30 PHP
PHP 验证码的实现代码
2011/07/17 PHP
浅析php中抽象类和接口的概念以及区别
2013/06/27 PHP
Yii控制器中filter过滤器用法分析
2016/07/15 PHP
详细解读php的命名空间(二)
2018/02/21 PHP
Yii2 中实现单点登录的方法
2018/03/09 PHP
Laravel 修改默认日志文件名称和位置的例子
2019/10/17 PHP
js修改table中Td的值(定义td的双击事件)
2013/01/10 Javascript
使用jquery局部刷新(jquery.load)从数据库取出数据
2014/01/22 Javascript
js获取页面传来参数的方法
2014/09/06 Javascript
JavaScript及jquey实现多个数组的合并操作
2014/09/06 Javascript
JS实现兼容性好,自动置顶的淘宝悬浮工具栏效果
2015/09/18 Javascript
JavaScript中的原始值和复杂值
2016/01/07 Javascript
Electron 调用命令行(cmd)
2019/09/23 Javascript
jQuery实现简易聊天框
2020/02/08 jQuery
vue2.x数组劫持原理的实现
2020/04/19 Javascript
Vue在H5 项目中使用融云进行实时个人单聊通讯
2020/12/14 Vue.js
用Python登录好友QQ空间点赞的示例代码
2017/11/04 Python
python 使用值来排序一个字典的方法
2018/11/16 Python
django 中QuerySet特性功能详解
2019/07/25 Python
Python类及获取对象属性方法解析
2020/06/15 Python
Pycharm创建文件时自动生成文件头注释(自定义设置作者日期)
2020/11/24 Python
10个python爬虫入门基础代码实例 + 1个简单的python爬虫完整实例
2020/12/16 Python
Camper鞋西班牙官方网上商店:西班牙马略卡岛的鞋类品牌
2019/03/14 全球购物
Linux文件操作命令都有哪些
2016/07/23 面试题
公司财务总监岗位职责
2013/12/14 职场文书
索桥的故事教学反思
2014/02/06 职场文书
幼儿园毕业家长感言
2014/02/10 职场文书
广告学专业自荐信范文
2014/02/24 职场文书
十八大观后感
2015/06/12 职场文书
羊脂球读书笔记
2015/06/30 职场文书
小学语文教学反思范文
2016/03/03 职场文书
六年级语文教学反思
2016/03/03 职场文书
2016年学校党支部创先争优活动总结
2016/04/05 职场文书
Python实现socket库网络通信套接字
2021/06/04 Python