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中闭包Closure函数作为返回值的方法示例
Dec 17 Python
浅谈flask截获所有访问及before/after_request修饰器
Jan 18 Python
Python如何抓取天猫商品详细信息及交易记录
Feb 23 Python
用Python解决x的n次方问题
Feb 08 Python
如何使用Python标准库进行性能测试
Jun 25 Python
Python调用graphviz绘制结构化图形网络示例
Nov 22 Python
python pycharm最新版本激活码(永久有效)附python安装教程
Sep 18 Python
Python如何定义接口和抽象类
Jul 28 Python
Python使用urlretrieve实现直接远程下载图片的示例代码
Aug 17 Python
基于Python的图像阈值化分割(迭代法)
Nov 20 Python
Python Pandas知识点之缺失值处理详解
May 11 Python
python turtle绘图
May 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
用DBSQL类加快开发MySQL数据库程序的速度
2006/10/09 PHP
PHP迭代器实现斐波纳契数列的函数
2013/11/12 PHP
PHP实现多文件上传的方法
2015/07/08 PHP
PHP7修改的函数
2021/03/09 PHP
Extjs 几个方法的讨论
2010/01/28 Javascript
Javascript中的delete介绍
2012/09/02 Javascript
javascript中拼接HTML字符串的最快、最好的方法
2014/06/07 Javascript
Javascript中的方法链(Method Chaining)介绍
2015/03/15 Javascript
JavaScript创建一个object对象并操作对象属性的用法
2015/03/23 Javascript
JS获取下拉框显示值和判断单选按钮的方法
2015/07/09 Javascript
JS给swf传参数的实现方法
2016/09/13 Javascript
巧用weui.topTips验证数据的实例
2017/04/17 Javascript
Three.js利用性能插件stats实现性能监听的方法
2017/09/25 Javascript
Node 自动化部署的方法
2017/10/17 Javascript
微信小程序实现添加手机联系人功能示例
2017/11/30 Javascript
将jquery.qqFace.js表情转换成微信的字符码
2017/12/01 jQuery
基于Vue实现微信小程序的图文编辑器
2018/07/25 Javascript
angular-tree-component的使用详解
2018/07/30 Javascript
Vue中实现回车键切换焦点的方法
2020/02/19 Javascript
vue cli4.0项目引入typescript的方法
2020/07/17 Javascript
[01:11:21]DOTA2-DPC中国联赛 正赛 VG vs Elephant BO3 第一场 3月6日
2021/03/11 DOTA
python批量下载图片的三种方法
2013/04/22 Python
利用Python爬取可用的代理IP
2016/08/18 Python
Pandas分组与排序的实现
2019/07/23 Python
python 定义类时,实现内部方法的互相调用
2019/12/25 Python
matplotlib bar()实现百分比堆积柱状图
2021/02/24 Python
Boolean b = new Boolean(“abcde”); 会编译错误码
2013/11/27 面试题
2015年幼儿园毕业感言
2014/02/12 职场文书
《哪吒闹海》教学反思
2014/02/28 职场文书
2014两会优秀的心得体会范文
2014/03/17 职场文书
服务标语口号
2014/07/01 职场文书
购房公证委托书(2014版)
2014/09/12 职场文书
党员个人对照检查材料思想汇报
2014/09/16 职场文书
党员对十八届四中全会的期盼思想汇报范文
2014/10/17 职场文书
2016年幼儿园教师政治学习心得体会
2016/01/23 职场文书
Python入门之基础语法详解
2021/05/11 Python