Python3中正则模块re.compile、re.match及re.search函数用法详解


Posted in Python onJune 11, 2018

本文实例讲述了Python3中正则模块re.compile、re.match及re.search函数用法。分享给大家供大家参考,具体如下:

re模块 re.compile、re.match、 re.search

import re
str = 'hello world! hello python'
pattern = re.compile(r'(?P<first>hell\w)(?P<symbol>\s)(?P<last>.*ld!)') # 分组,0 组是整个 hello world!, 1组 hello,2组 ld!
match = re.match(pattern, str)
print('group 0:', match.group(0)) # 匹配 0 组,整个字符串
print('group 1:', match.group(1)) # 匹配第一组,hello
print('group 2:', match.group(2)) # 匹配第二组,空格
print('group 3:', match.group(3)) # 匹配第三组,ld!
print('groups:', match.groups())  # groups 方法,返回一个包含所有分组匹配的元组
print('start 0:', match.start(0), 'end 0:', match.end(0)) # 整个匹配开始和结束的索引值
print('start 1:', match.start(1), 'end 1:', match.end(1)) # 第一组开始和结束的索引值
print('start 2:', match.start(1), 'end 2:', match.end(2)) # 第二组开始和结束的索引值
print('pos 开始于:', match.pos)
print('endpos 结束于:', match.endpos) # string 的长度
print('lastgroup 最后一个被捕获的分组的名字:', match.lastgroup)
print('lastindex 最后一个分组在文本中的索引:', match.lastindex)
print('string 匹配时候使用的文本:', match.string)
print('re 匹配时候使用的 Pattern 对象:', match.re)
print('span 返回分组匹配的 index (start(group),end(group)):', match.span(2))

返回结果:

group 0: hello world!
group 1: hello
group 2: 
group 3: world!
groups: ('hello', ' ', 'world!')
start 0: 0 end 0: 12
start 1: 0 end 1: 5
start 2: 0 end 2: 6
pos 开始于: 0
endpos 结束于: 25
lastgroup 最后一个被捕获的分组的名字: last
lastindex 最后一个分组在文本中的索引: 3
string 匹配时候使用的文本: hello world! hello python
re 匹配时候使用的 Pattern 对象: re.compile('(?P<first>hell\\w)(?P<symbol>\\s)(?P<last>.*ld!)')
span 返回分组匹配的 index (start(group),end(group)): (5, 6)

re.search 函数

对整个字符串进行搜索匹配,返回第一个匹配的字符串的 match 对象。

re.search(pattern, string[, flags=0])

  • pattern 匹配模式,由 re.compile 获得
  • string 需要匹配的字符串
import re
str = 'say hello world! hello python'
pattern = re.compile(r'(?P<first>hell\w)(?P<symbol>\s)(?P<last>.*ld!)') # 分组,0 组是整个 hello world!, 1组 hello,2组 ld!
search = re.search(pattern, str)
print('group 0:', search.group(0)) # 匹配 0 组,整个字符串
print('group 1:', search.group(1)) # 匹配第一组,hello
print('group 2:', search.group(2)) # 匹配第二组,空格
print('group 3:', search.group(3)) # 匹配第三组,ld!
print('groups:', search.groups())  # groups 方法,返回一个包含所有分组匹配的元组
print('start 0:', search.start(0), 'end 0:', search.end(0)) # 整个匹配开始和结束的索引值
print('start 1:', search.start(1), 'end 1:', search.end(1)) # 第一组开始和结束的索引值
print('start 2:', search.start(1), 'end 2:', search.end(2)) # 第二组开始和结束的索引值
print('pos 开始于:', search.pos)
print('endpos 结束于:', search.endpos) # string 的长度
print('lastgroup 最后一个被捕获的分组的名字:', search.lastgroup)
print('lastindex 最后一个分组在文本中的索引:', search.lastindex)
print('string 匹配时候使用的文本:', search.string)
print('re 匹配时候使用的 Pattern 对象:', search.re)
print('span 返回分组匹配的 index (start(group),end(group)):', search.span(2))

注意 re.search 和 re.match 匹配的 str 的区别

打印结果:

group 0: hello world!
group 1: hello
group 2: 
group 3: world!
groups: ('hello', ' ', 'world!')
start 0: 4 end 0: 16
start 1: 4 end 1: 9
start 2: 4 end 2: 10
pos 开始于: 0
endpos 结束于: 29
lastgroup 最后一个被捕获的分组的名字: last
lastindex 最后一个分组在文本中的索引: 3
string 匹配时候使用的文本: say hello world! hello python
re 匹配时候使用的 Pattern 对象: re.compile('(?P<first>hell\\w)(?P<symbol>\\s)(?P<last>.*ld!)')
span 返回分组匹配的 index (start(group),end(group)): (9, 10)

Python 相关文章推荐
Python可跨平台实现获取按键的方法
Mar 05 Python
Python使用MYSQLDB实现从数据库中导出XML文件的方法
May 11 Python
Python实现统计单词出现的个数
May 28 Python
Python基于回溯法子集树模板实现图的遍历功能示例
Sep 05 Python
利用python画出折线图
Jul 26 Python
面向初学者的Python编辑器Mu
Oct 08 Python
Python 实例方法、类方法、静态方法的区别与作用
Aug 14 Python
简单了解django文件下载方式
Feb 10 Python
Pytorch maxpool的ceil_mode用法
Feb 18 Python
如何解决安装python3.6.1失败
Jul 01 Python
python将下载到本地m3u8视频合成MP4的代码详解
Nov 24 Python
python的dict判断key是否存在的方法
Dec 09 Python
python检测空间储存剩余大小和指定文件夹内存占用的实例
Jun 11 #Python
Python3多进程 multiprocessing 模块实例详解
Jun 11 #Python
Python3中的列表生成式、生成器与迭代器实例详解
Jun 11 #Python
python xlsxwriter创建excel图表的方法
Jun 11 #Python
python操作excel的包(openpyxl、xlsxwriter)
Jun 11 #Python
django 使用 request 获取浏览器发送的参数示例代码
Jun 11 #Python
python操作excel的方法(xlsxwriter包的使用)
Jun 11 #Python
You might like
无JS,完全php面向过程数据分页实现代码
2012/08/27 PHP
探讨fckeditor在Php中的配置详解
2013/06/08 PHP
PHP file_get_contents设置超时处理方法
2013/09/30 PHP
php正则判断是否为合法身份证号的方法
2017/03/16 PHP
php DES加密算法实例分析
2019/09/18 PHP
js获取变量
2006/08/24 Javascript
javascript实现二分查找法实现代码
2007/11/12 Javascript
js 获取中文拼音,Select自动匹配字母获取值的代码
2009/09/23 Javascript
jquery实现marquee效果(文字或者图片的水平垂直滚动)
2013/01/07 Javascript
解析javascript 数组以及json元素的添加删除
2013/06/26 Javascript
简约JS日历控件 实例代码
2013/07/12 Javascript
js中的异常处理try...catch使用介绍
2013/09/21 Javascript
JQuery异步加载无限下拉框级联功能实现示例
2014/02/19 Javascript
jQuery实现瀑布流布局
2014/12/12 Javascript
ajax如何实现页面局部跳转与结果返回
2015/08/24 Javascript
推荐阅读的js快速判断IE浏览器(兼容IE10与IE11)
2015/12/13 Javascript
阿里巴巴技术文章分享 Javascript继承机制的实现
2016/01/14 Javascript
javascript url几种编码方式详解
2016/06/06 Javascript
windows下vue-cli及webpack搭建安装环境
2017/04/25 Javascript
BootStrap Table实现server分页序号连续显示功能(当前页从上一页的结束序号开始)
2017/09/12 Javascript
利用JS测试目标网站的打开响应速度
2017/12/01 Javascript
使用Ajax和Jquery配合数据库实现下拉框的二级联动的示例
2018/01/25 jQuery
微信小程序实现手指触摸画板
2018/07/09 Javascript
JavaScript中call和apply方法的区别实例分析
2018/08/03 Javascript
vue+axios全局添加请求头和参数操作
2020/07/24 Javascript
Python编程实现从字典中提取子集的方法分析
2018/02/09 Python
深入理解Python 关于supper 的 用法和原理
2018/02/28 Python
python修改txt文件中的某一项方法
2018/12/29 Python
使用PIL(Python-Imaging)反转图像的颜色方法
2019/01/24 Python
pytorch多进程加速及代码优化方法
2019/08/19 Python
Tensorflow获取张量Tensor的具体维数实例
2020/01/19 Python
音乐教师求职信
2014/06/28 职场文书
税务干部群众路线教育实践活动自我剖析材料
2014/09/21 职场文书
公司离职证明标准样本
2014/10/05 职场文书
Django利用AJAX技术实现博文实时搜索
2021/05/06 Python
python实现学员管理系统(面向对象版)
2022/06/05 Python