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 相关文章推荐
在NumPy中创建空数组/矩阵的方法
Jun 15 Python
对pycharm 修改程序运行所需内存详解
Dec 03 Python
Python 实现域名解析为ip的方法
Feb 14 Python
python 通过SSHTunnelForwarder隧道连接redis的方法
Feb 19 Python
Python3实现的简单三级菜单功能示例
Mar 12 Python
PyQt5通信机制 信号与槽详解
Aug 07 Python
python GUI库图形界面开发之PyQt5表单布局控件QFormLayout详细使用方法与实例
Mar 06 Python
python sitk.show()与imageJ结合使用常见的问题
Apr 20 Python
Python urllib.request对象案例解析
May 11 Python
Python多线程的退出控制实现
Aug 10 Python
python如何实现递归转非递归
Feb 25 Python
Python Numpy之linspace用法说明
Apr 17 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
用PHP实现浏览器点击下载TXT文档的方法详解
2013/06/02 PHP
php将数组存储为文本文件方法汇总
2015/10/28 PHP
php实现遍历多维数组的方法
2015/11/25 PHP
微信开发之网页授权获取用户信息(二)
2016/01/08 PHP
php获取文章内容第一张图片的方法示例
2017/07/03 PHP
JS中彻底删除JSON对象组成的数组中的元素
2020/09/22 PHP
浅析onsubmit校验表单时利用ajax的return false无效问题
2013/07/10 Javascript
node.js中的fs.openSync方法使用说明
2014/12/17 Javascript
JS模拟并美化的表单控件完整实例
2015/08/19 Javascript
JavaScript中各种引用类型的常用操作方法小结
2016/05/05 Javascript
移动端翻页插件dropload.js(支持Zepto和jQuery)
2016/07/27 Javascript
js实现对table的增加行和删除行的操作方法
2016/10/13 Javascript
Bootstrap警告框(Alert)插件使用方法
2017/03/21 Javascript
Angularjs实现控制器之间通信方式实例总结
2018/03/27 Javascript
vue项目初始化到登录login页面的示例
2019/10/31 Javascript
Jquery+javascript实现支付网页数字键盘
2020/12/21 jQuery
Python和php通信乱码问题解决方法
2014/04/15 Python
Python3处理文件中每个词的方法
2015/05/22 Python
解决pyqt中ui编译成窗体.py中文乱码的问题
2016/12/23 Python
Windows下python3.6.4安装教程
2018/07/31 Python
CentOS 7 安装python3.7.1的方法及注意事项
2018/11/01 Python
python ChainMap的使用和说明详解
2019/06/11 Python
Python爬虫 scrapy框架爬取某招聘网存入mongodb解析
2019/07/31 Python
ansible-playbook实现自动部署KVM及安装python3的详细教程
2020/05/11 Python
pytorch加载语音类自定义数据集的方法教程
2020/11/10 Python
HTML5给汉字加拼音收起展开组件的实现代码
2020/04/08 HTML / CSS
美国五金商店:Ace Hardware
2018/03/27 全球购物
数据库专业英语
2012/11/30 面试题
应届生文秘专业个人自荐信格式
2013/09/21 职场文书
财务管理专业毕业生求职信范文
2013/09/21 职场文书
代理商会议邀请函
2014/01/27 职场文书
办公室主任主任岗位责任制
2014/02/11 职场文书
平面设计求职信
2014/03/10 职场文书
单位委托书怎么写
2014/09/21 职场文书
高三物理教学反思
2016/02/20 职场文书
幼儿园教师辞职信
2019/06/21 职场文书