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读取环境变量的方法和自定义类分享
Nov 22 Python
Python的pycurl包用法简介
Nov 13 Python
PyCharm使用教程之搭建Python开发环境
Jun 07 Python
pycharm创建一个python包方法图解
Apr 10 Python
python调用webservice接口的实现
Jul 12 Python
python psutil模块使用方法解析
Aug 01 Python
使用Windows批处理和WMI设置Python的环境变量方法
Aug 14 Python
opencv-python 提取sift特征并匹配的实例
Dec 09 Python
Python列表切片常用操作实例解析
Mar 10 Python
一文解决django 2.2与mysql兼容性问题
Jul 15 Python
Python 数据的累加与统计的示例代码
Aug 03 Python
python数字图像处理实现图像的形变与缩放
Jun 28 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/06/09 PHP
php&amp;mysql 日期操作小记
2012/02/27 PHP
PHP图片等比例缩放生成缩略图函数分享
2014/06/10 PHP
PHP数组遍历知识汇总(包含遍历方法、数组指针操作函数、数组遍历测速)
2014/07/05 PHP
PHP动态柱状图实现方法
2015/03/30 PHP
php源码之将图片转化为data/base64数据流实例详解
2016/11/27 PHP
JavaScript 内置对象属性及方法集合
2010/07/04 Javascript
JS+CSS实现滑动切换tab菜单效果
2015/08/25 Javascript
js如何判断输入字符串长度
2015/12/16 Javascript
JavaScript操作HTML DOM节点的基础教程
2016/03/11 Javascript
javascript宿主对象之window.navigator详解
2016/09/07 Javascript
bootstrap的3级菜单样式,支持母版页保留打开状态实现方法
2016/11/10 Javascript
jQuery EasyUi 验证功能实例解析
2017/01/06 Javascript
bootstrap输入框组件使用方法详解
2017/01/19 Javascript
vue实现类似淘宝商品评价页面星级评价及上传多张图片功能
2018/10/29 Javascript
Vue项目vscode 安装eslint插件的方法(代码自动修复)
2020/04/15 Javascript
mapboxgl区划标签避让不遮盖实现的代码详解
2020/07/01 Javascript
使用vue实现通过变量动态拼接url
2020/07/22 Javascript
[57:18]DOTA2上海特级锦标赛主赛事日 - 1 败者组第一轮#3VP VS VG
2016/03/03 DOTA
python中enumerate函数用法实例分析
2015/05/20 Python
解决python xlrd无法读取excel文件的问题
2018/12/25 Python
ZABBIX3.2使用python脚本实现监控报表的方法
2019/07/02 Python
Django表单提交后实现获取相同name的不同value值
2020/05/14 Python
python爬取抖音视频的实例分析
2021/01/19 Python
python使用scapy模块实现ARP扫描的过程
2021/01/21 Python
浅析几个CSS3常用功能的写法
2014/06/05 HTML / CSS
详解如何使用rem或viewport进行移动端适配
2020/08/14 HTML / CSS
八皇后问题,输出了所有情况,不过有些结果只是旋转了90度
2016/08/15 面试题
欢迎领导检查标语
2014/06/27 职场文书
应届大学生求职信
2014/07/20 职场文书
小学中队长竞选稿
2015/11/20 职场文书
读《推着妈妈去旅行》有感1500字
2019/10/15 职场文书
解决Jupyter-notebook不弹出默认浏览器的问题
2021/03/30 Python
python中的plt.cm.Paired用法说明
2021/05/31 Python
Python加密与解密模块hashlib与hmac
2022/06/05 Python
nginx配置指令之server_name的具体使用
2022/08/14 Servers