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为tornado添加recaptcha验证码功能
Feb 26 Python
python使用正则搜索字符串或文件中的浮点数代码实例
Jul 11 Python
python简单实现刷新智联简历
Mar 30 Python
python+django加载静态网页模板解析
Dec 12 Python
Python cookbook(数据结构与算法)在字典中将键映射到多个值上的方法
Feb 18 Python
python读取一个目录下所有txt里面的内容方法
Jun 23 Python
基于Python实现定时自动给微信好友发送天气预报
Oct 25 Python
python保存字典和读取字典的实例代码
Jul 07 Python
使用Pandas的Series方法绘制图像教程
Dec 04 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
May 26 Python
python dict乱码如何解决
Jun 07 Python
python中os.remove()用法及注意事项
Jan 31 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 文件上传类代码
2011/08/06 PHP
PHP使用Redis替代文件存储Session的方法
2017/02/15 PHP
比较搞笑的js陷阱题
2010/02/07 Javascript
js 优化次数过多的循环 考虑到性能问题
2011/03/05 Javascript
Draggable Elements 元素拖拽功能实现代码
2011/03/30 Javascript
基于JQUERY的两个ListBox子项互相调整的实现代码
2011/05/07 Javascript
IE关闭时判断及AJAX注销案例学习
2013/02/18 Javascript
JQuery实现倒计时按钮具体方法
2013/11/14 Javascript
谈谈javascript中使用连等赋值操作带来的问题
2015/11/26 Javascript
借助FileReader实现将文件编码为Base64后通过AJAX上传
2015/12/24 Javascript
javascript实现倒计时跳转页面
2016/01/17 Javascript
jQuery实现为LI列表前3行设置样式的方法【2种方法】
2016/09/04 Javascript
jQuery通过ajax方法获取json数据不执行success的原因及解决方法
2016/10/15 Javascript
Vuejs 页面的区域化与组件封装的实现
2017/09/11 Javascript
vue 微信授权登录解决方案
2018/04/10 Javascript
javascript获取元素的计算样式
2019/05/24 Javascript
JavaScript获取时区实现过程解析
2020/09/24 Javascript
JavaScript实现下拉列表
2021/01/20 Javascript
python使用webbrowser浏览指定url的方法
2015/04/04 Python
Python使用QQ邮箱发送Email的方法实例
2017/02/09 Python
浅谈numpy生成数组的零值问题
2018/11/12 Python
浅谈keras通过model.fit_generator训练模型(节省内存)
2020/06/17 Python
python按照list中字典的某key去重的示例代码
2020/10/13 Python
10个示例带你掌握python中的元组
2020/11/23 Python
CSS3媒体查询(Media Queries)介绍
2013/09/12 HTML / CSS
CSS3 Flex 弹性布局实例代码详解
2018/11/01 HTML / CSS
html5教程画矩形代码分享
2013/12/04 HTML / CSS
美国滑板店:Tactics
2020/11/08 全球购物
软件测试工程师结构化面试题库
2016/11/23 面试题
好邻里事迹材料
2014/01/16 职场文书
司机检讨书
2014/02/13 职场文书
学习演讲稿范文
2014/05/10 职场文书
手术室护士节演讲稿
2014/08/27 职场文书
搞笑老公保证书
2015/02/26 职场文书
golang 定时任务方面time.Sleep和time.Tick的优劣对比分析
2021/05/05 Golang
微信小程序基础教程之echart的使用
2021/06/01 Javascript