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进程的信号通讯
Apr 09 Python
Python生成器(Generator)详解
Apr 13 Python
Python正则获取、过滤或者替换HTML标签的方法
Jan 28 Python
Django实现快速分页的方法实例
Oct 22 Python
python实现定时压缩指定文件夹发送邮件
Dec 22 Python
使用Python实现跳一跳自动跳跃功能
Jul 10 Python
执行Django数据迁移时报 1091错误及解决方法
Oct 14 Python
解决import tensorflow as tf 出错的原因
Apr 16 Python
python代码中怎么换行
Jun 17 Python
为什么是 Python -m
Jun 19 Python
python基于socket模拟实现ssh远程执行命令
Dec 05 Python
PyTorch中permute的使用方法
Apr 26 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中的超全局变量
2006/10/09 PHP
Php Cookie的一个使用注意点
2008/11/08 PHP
五款PHP代码重构工具推荐
2014/10/14 PHP
php实现删除空目录的方法
2015/03/16 PHP
Yii配置与使用memcached缓存的方法
2016/07/13 PHP
prototype Element学习笔记(篇二)
2008/10/26 Javascript
JQuery中对Select的option项的添加、删除、取值
2013/08/25 Javascript
jquery操作复选框(checkbox)的12个小技巧总结
2014/02/04 Javascript
浏览器窗口大小变化时使用resize事件对框架不起作用的解决方法
2014/05/11 Javascript
js实现按一下删除键删除整个单词附demo
2014/09/05 Javascript
js监听键盘事件的方法_原生和jquery的区别详解
2016/10/10 Javascript
jQuery实现ToolTip元素定位显示功能示例
2016/11/23 Javascript
JS原型与原型链的深入理解
2017/02/15 Javascript
使用jquery的jsonp如何发起跨域请求及其原理详解
2017/08/17 jQuery
微信小程序实现人脸识别
2018/05/25 Javascript
JavaScript设计模式之代理模式简单实例教程
2018/07/03 Javascript
微信小程序scroll-view实现滚动穿透和阻止滚动的方法
2018/08/20 Javascript
微信小程序实现顶部导航特效
2019/01/28 Javascript
详解使用React制作一个模态框
2019/03/14 Javascript
浅谈Javascript中的对象和继承
2019/04/19 Javascript
用webpack4开发小程序的实现方法
2019/06/04 Javascript
使用Vue.js 和Chart.js制作绚丽多彩的图表
2019/06/15 Javascript
antd-DatePicker组件获取时间值,及相关设置方式
2020/10/27 Javascript
原生JavaScript实现轮播图
2021/01/10 Javascript
python之import机制详解
2014/07/03 Python
python3编写ThinkPHP命令执行Getshell的方法
2019/02/26 Python
使用pycharm在本地开发并实时同步到服务器
2019/08/02 Python
Python 给下载文件显示进度条和下载时间的实现
2020/04/02 Python
用css3实现转换过渡和动画效果
2020/03/13 HTML / CSS
祖国在我心中演讲稿
2014/01/15 职场文书
2014年基层党建工作总结
2014/11/11 职场文书
八一建军节慰问信
2015/02/14 职场文书
2015年员工试用期工作总结
2015/05/28 职场文书
创业计划书之农家乐
2019/10/09 职场文书
LayUI+Shiro实现动态菜单并记住菜单收展的示例
2021/05/06 Javascript
vue css 相对路径导入问题级踩坑记录
2022/06/05 Vue.js