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读取html中指定元素生成excle文件示例
Apr 03 Python
Python查找相似单词的方法
Mar 05 Python
Linux RedHat下安装Python2.7开发环境
May 20 Python
人机交互程序 python实现人机对话
Nov 14 Python
python针对excel的操作技巧
Mar 13 Python
配置 Pycharm 默认 Test runner 的图文教程
Nov 30 Python
利用Python脚本实现自动刷网课
Feb 03 Python
Python实现验证码识别
Jun 15 Python
flask项目集成swagger的方法
Dec 09 Python
Pytorch 中net.train 和 net.eval的使用说明
May 22 Python
解决pycharm安装scrapy DLL load failed:找不到指定的程序的问题
Jun 08 Python
Python之matplotlib绘制折线图
Apr 13 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
这东西价格,可以买几台TECSUN S-2000
2021/03/02 无线电
解决PHP在DOS命令行下却无法链接MySQL的技术笔记
2010/12/29 PHP
str_replace只替换一次字符串的方法
2013/04/09 PHP
php使用filter过滤器验证邮箱 ipv6地址 url验证
2013/12/25 PHP
php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
2020/04/05 PHP
PHP合并数组+号和array_merge的区别
2015/06/25 PHP
PHP文件操作之获取目录下文件与计算相对路径的方法
2016/01/08 PHP
php+ajax实现带进度条的上传图片功能【附demo源码下载】
2016/09/14 PHP
php异步:在php中使用fsockopen curl实现类似异步处理的功能方法
2016/12/10 PHP
让广告代码不再影响你的网页加载速度
2006/07/07 Javascript
动态改变textbox的宽高的js
2006/10/26 Javascript
ExtJs Excel导出并下载IIS服务器端遇到的问题
2011/09/16 Javascript
jquery简单的拖动效果实现原理及示例
2013/07/26 Javascript
Jquery 实现表格颜色交替变化鼠标移过颜色变化实例
2013/08/28 Javascript
自己编写的类似JS的trim方法
2013/10/09 Javascript
JavaScript中字符串分割函数split用法实例
2015/04/07 Javascript
jQuery打字效果实现方法(附demo源码下载)
2015/12/18 Javascript
JS继承之借用构造函数继承和组合继承
2016/09/07 Javascript
深入理解Node module模块
2018/03/26 Javascript
JavaScript中变量、指针和引用功能与操作示例
2018/08/04 Javascript
Vue刷新修改页面中数据的方法
2018/09/16 Javascript
解决ant design vue 表格a-table二次封装,slots渲染的问题
2020/10/28 Javascript
js删除指定位置超链接中含有百度与360的标题
2021/01/06 Javascript
Python实现telnet服务器的方法
2015/07/10 Python
Flask框架的学习指南之制作简单blog系统
2016/11/20 Python
django轻松使用富文本编辑器CKEditor的方法
2017/03/30 Python
详解Python:面向对象编程
2019/04/10 Python
tensorflow 实现自定义layer并添加到计算图中
2020/02/04 Python
python绘制封闭多边形教程
2020/02/18 Python
奥地利网上现代灯具和灯饰店:Lampenwelt.at
2018/01/29 全球购物
彪马西班牙官网:PUMA西班牙
2019/06/18 全球购物
客服专员岗位职责范本
2013/11/29 职场文书
医院护士的求职信
2014/01/03 职场文书
学生爱国演讲稿
2014/01/14 职场文书
自习课吵闹检讨书范文
2014/09/26 职场文书
先进集体申报材料
2014/12/25 职场文书