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 04 Python
跟老齐学Python之list和str比较
Sep 20 Python
Python中用post、get方式提交数据的方法示例
Sep 22 Python
Python字符串拼接六种方法介绍
Dec 18 Python
python实现n个数中选出m个数的方法
Nov 13 Python
在Python中输入一个以空格为间隔的数组方法
Nov 13 Python
通过pycharm使用git的步骤(图文详解)
Jun 13 Python
解决Pytorch 训练与测试时爆显存(out of memory)的问题
Aug 20 Python
python实现最速下降法
Mar 24 Python
浅谈python 中的 type(), dtype(), astype()的区别
Apr 09 Python
有关pycharm登录github时有的时候会报错connection reset的问题
Sep 15 Python
selenium学习教程之定位以及切换frame(iframe)
Jan 04 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/01/14 PHP
使用 PHPMAILER 发送邮件实例应用
2012/11/07 PHP
PHP PDOStatement::nextRowset讲解
2019/02/01 PHP
Laravel 模型使用软删除-左连接查询-表起别名示例
2019/10/24 PHP
PHP sdk文档处理常用代码示例解析
2020/12/09 PHP
防止文件缓存的js代码
2013/01/10 Javascript
window.requestAnimationFrame是什么意思,怎么用
2013/01/13 Javascript
js 实现日期灵活格式化的小例子
2013/07/14 Javascript
JS基于面向对象实现的放烟花效果
2015/05/07 Javascript
JavaScript中的getTimezoneOffset()方法使用详解
2015/06/10 Javascript
JavaScript中数据结构与算法(四):串(BF)
2015/06/19 Javascript
使用jQuery.form.js/springmvc框架实现文件上传功能
2016/05/12 Javascript
JavaScript实现点击文本自动定位到下拉框选中操作
2016/06/15 Javascript
如何解决hover在ie6中的兼容性问题
2016/12/15 Javascript
纯html+css+javascript实现楼层跳跃式的页面布局(实例代码)
2017/10/25 Javascript
jQuery zTree插件使用简单教程
2019/08/16 jQuery
python使用cStringIO实现临时内存文件访问的方法
2015/03/26 Python
Python 3实战爬虫之爬取京东图书的图片详解
2017/10/09 Python
详解python使用Nginx和uWSGI来运行Python应用
2018/01/09 Python
Django项目开发中cookies和session的常用操作分析
2018/07/03 Python
Python3.7 dataclass使用指南小结
2019/02/22 Python
python处理自动化任务之同时批量修改word里面的内容的方法
2019/08/23 Python
python中的selenium安装的步骤(浏览器自动化测试框架)
2020/03/17 Python
解决Python安装cryptography报错问题
2020/09/03 Python
Bata印度官网:源自欧洲舒适鞋履品牌
2020/01/30 全球购物
请问如下代码执行后a和b的值分别是什么
2016/05/05 面试题
2015年安全生产目标责任书
2015/01/29 职场文书
最感人的道歉情书
2015/05/12 职场文书
六一儿童节主持开场白
2015/05/28 职场文书
博士论文答辩开场白
2015/06/01 职场文书
污染环境建议书
2015/09/14 职场文书
2019思想汇报范文
2019/05/21 职场文书
导游词之西湖雷峰塔
2019/09/18 职场文书
python本地文件服务器实例教程
2021/05/02 Python
使用CSS3实现按钮悬停闪烁动态特效代码
2021/08/30 HTML / CSS
解决Springboot PostMapping无法获取数据的问题
2022/05/06 Java/Android