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制作一个桌面便签软件
Aug 09 Python
Ubuntu安装Jupyter Notebook教程
Oct 18 Python
一文带你了解Python中的字符串是什么
Nov 20 Python
python统计中文字符数量的两种方法
Jan 31 Python
python opencv鼠标事件实现画框圈定目标获取坐标信息
Apr 18 Python
对python中UDP,socket的使用详解
Aug 22 Python
基于梯度爆炸的解决方法:clip gradient
Feb 04 Python
快速解决Django关闭Debug模式无法加载media图片与static静态文件
Apr 07 Python
使用Python FastAPI构建Web服务的实现
Jun 08 Python
PyTorch 导数应用的使用教程
Aug 31 Python
Python 利用argparse模块实现脚本命令行参数解析
Dec 28 Python
关于的python五子棋的算法
May 02 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
用cookies来跟踪识别用户
2006/10/09 PHP
杏林同学录(五)
2006/10/09 PHP
PHP 5.3 下载时 VC9、VC6、Thread Safe、Non Thread Safe的区别分析
2011/03/28 PHP
php 目录遍历、删除 函数的使用介绍
2013/04/28 PHP
Yii中实现处理前后台登录的新方法
2015/12/28 PHP
php关闭warning问题的解决方法
2016/05/17 PHP
PHP实现原生态图片上传封装类方法
2016/11/08 PHP
关于PHP转换超过2038年日期出错的问题解决
2017/06/28 PHP
PHP7导出Excel报ERR_EMPTY_RESPONSE解决方法
2019/04/16 PHP
JQuery 前台切换网站的样式实现
2009/06/22 Javascript
window.onbeforeunload方法在IE下无法正常工作的解决办法
2010/01/23 Javascript
JavaScript Scoping and Hoisting 翻译
2012/07/03 Javascript
基于jquery自己写tab滑动门(通用版)
2012/10/30 Javascript
微信小程序 图片宽度自适应的实现
2017/04/06 Javascript
webpack开发跨域问题解决办法
2017/08/03 Javascript
js实现上传图片并显示图片名称
2019/12/18 Javascript
JS实现鼠标按下拖拽效果
2020/07/23 Javascript
python有证书的加密解密实现方法
2014/11/19 Python
举例简单讲解Python中的数据存储模块shelve的用法
2016/03/03 Python
详解Python 装饰器执行顺序迷思
2018/08/08 Python
python爬取淘宝商品销量信息
2018/11/16 Python
python3使用print打印带颜色的字符串代码实例
2019/08/22 Python
TensorFlow实现自定义Op方式
2020/02/04 Python
Python2.6版本pip安装步骤解析
2020/08/17 Python
CSS3 border-image详解、应用及jQuery插件
2011/08/29 HTML / CSS
CSS伪类与CSS伪元素的区别及由来具体说明
2012/12/07 HTML / CSS
使用html5实现表格实现标题合并的实例代码
2019/05/13 HTML / CSS
美国最流行的男士时尚网站:Touch of Modern
2018/02/05 全球购物
关联、聚合(Aggregation)以及组合(Composition)的区别
2012/02/29 面试题
《唯一的听众》教学反思
2014/02/20 职场文书
民主评议党员自我评价材料
2014/09/18 职场文书
大学毕业论文致谢词
2015/05/14 职场文书
爱护环境建议书
2015/09/14 职场文书
2016大学生暑期社会实践心得体会
2016/01/14 职场文书
演讲稿:态度决定一切
2019/04/02 职场文书
python前后端自定义分页器
2022/04/13 Python