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处理json数据中的中文
Mar 06 Python
python使用正则表达式匹配字符串开头并打印示例
Jan 11 Python
python机器学习库常用汇总
Nov 15 Python
django 2.0更新的10条注意事项总结
Jan 05 Python
python中yaml配置文件模块的使用详解
Apr 27 Python
通过python实现随机交换礼物程序详解
Jul 10 Python
Django框架模型简单介绍与使用分析
Jul 18 Python
python3中的logging记录日志实现过程及封装成类的操作
May 12 Python
解决Python数据可视化中文部分显示方块问题
May 16 Python
Django如何使用redis作为缓存
May 21 Python
matplotlib基础绘图命令之errorbar的使用
Aug 13 Python
python 实现超级玛丽游戏
Nov 25 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实现链式操作的核心思想
2015/06/23 PHP
在Mac OS的PHP环境下安装配置MemCache的全过程解析
2016/02/15 PHP
Thinkphp框架开发移动端接口(1)
2016/08/18 PHP
php加密之discuz内容经典加密方式实例详解
2017/02/04 PHP
PHP自定义错误处理的方法分析
2018/12/19 PHP
php ZipArchive实现多文件打包下载实例
2019/10/31 PHP
表单的一些基本用法与技巧
2006/07/15 Javascript
js下写一个事件队列操作函数
2010/07/19 Javascript
js屏蔽鼠标键盘(右键/Ctrl+N/Shift+F10/F11/F5刷新/退格键)
2013/01/24 Javascript
js实现动态改变字体大小代码
2014/01/02 Javascript
JS合并数组的几种方法及优劣比较
2014/09/19 Javascript
js 获取元素所有兄弟节点的实现方法
2016/09/06 Javascript
nodejs body-parser 解析post数据实例
2017/07/26 NodeJs
微信小程序 swiper组件构建轮播图的实例
2017/09/20 Javascript
vue生成token保存在客户端localStorage中的方法
2017/10/25 Javascript
微信小程序动态添加view组件的实例代码
2019/05/23 Javascript
微信小程序实现蒙版弹出窗功能
2019/09/17 Javascript
JS在Array数组中按指定位置删除或添加元素对象方法示例
2019/11/19 Javascript
js中火星坐标、百度坐标、WGS84坐标转换实现方法示例
2020/03/02 Javascript
Python对List中的元素排序的方法
2018/04/01 Python
学生信息管理系统python版
2018/10/17 Python
Python socket实现多对多全双工通信的方法
2019/02/13 Python
Python 2/3下处理cjk编码的zip文件的方法
2019/04/26 Python
django中forms组件的使用与注意
2019/07/08 Python
解决python 读取 log日志的编码问题
2019/12/24 Python
PyCharm中关于安装第三方包的三个建议
2020/09/17 Python
Django中和时区相关的安全问题详解
2020/10/12 Python
python 自动识别并连接串口的实现
2021/01/19 Python
浅析HTML5 meta viewport参数
2020/10/28 HTML / CSS
以设计师精品品质提供快速时尚:PopJulia
2018/01/09 全球购物
美国在线和移动免费会员制批发零售商:Boxed(移动端的Costco)
2020/01/02 全球购物
植树造林的宣传标语
2014/06/23 职场文书
关于教师节的演讲稿
2014/09/04 职场文书
2016年社区六一儿童节活动总结
2016/04/06 职场文书
七年级作文之秋游
2019/10/21 职场文书
「SHOW BY ROCK!!」“雫シークレットマインド”组合单曲MV公开
2022/03/21 日漫