python re.match()用法相关示例


Posted in Python onJanuary 27, 2021

学习python爬虫时遇到了一个问题,书上有示例如下:

import re

line='Cats are smarter than dogs'
matchObj=re.match(r'(.*)are(.*?).*',line)

if matchObj:
 print('matchObj.group():',matchObj.group())
 print('matchObj.group(1):', matchObj.group(1))
 print('matchObj.group(2):', matchObj.group(2))
else:
 print('No match!\n')

书上的期望输出是:

matchObj.group(): Cats are smarter than dogs
matchObj.group(1): Cats
matchObj.group(2):smarter

但是我在电脑上跑了一遍得到的输出却是:

matchObj.group(): Cats are smarter than dogs
matchObj.group(1): Cats
matchObj.group(2):

于是开始想办法彻底搞清楚这个差别的原因所在。

首先要读懂这几行代码,而这一行代码的关键在于这一句:

matchObj=re.match(r'(.*)are(.*?).*',line)

匹配的正则表达式是

(.*)are(.*?).*
前面的r表示的是匹配的字符不进行转义,而要匹配的字符串是line,也就是
Cats are smarter than dogs
后面使用group(num),个人理解是,按照正则表达式中的括号数可以捕获得到对应数量的捕获组,而调用group(num)就可以得到对应捕获组的内容,
其中group(0)表示的是匹配的整个表达式的字符串,在本例中就是‘Cats are smarter than dogs'。
参照网上可以搜到的符号的作用:
.匹配除换行符以外的任意字符
*重复之前的字符零次或更多次
?重复之前的字符零次或一次
那么第一个括号的内容,应当就是匹配要匹配的字符串中are之前的所有字符(除换行符),
而第二个括号的内容应当是匹配are之后的内容,但具体想指代什么却显得有些不明确。
不明确的点就在于*和?这两个符号的连用,根据优先级这两个符号是同一优先级的,那么应当按照顺序生效,那么如此翻译的话,这一语句匹配的就是长度为0到无限大的任意字符串,为了探清此时
程序判断的具体内容,我们给匹配字符串末尾的.*也加上括号以提取其内容,而后在输出部分加上对应语句:

import re

line='Cats are smarter than dogs'
matchObj=re.match(r'(.*)are(.*?)(.*)',line)

if matchObj:
 print("matchObj.group():",matchObj.group())
 print("matchObj.group(1):", matchObj.group(1))
 print("matchObj.group(2):", matchObj.group(2))
 print("matchObj.group(3):", matchObj.group(3))
else:
 print('No match!\n')

得到的结果是:

matchObj.group(): Cats are smarter than dogs
matchObj.group(1): Cats
matchObj.group(2):
matchObj.group(3):  smarter than dogs

可见第二个括号里的内容被默认为空了,然后删去那个?,可以看到结果变成:

matchObj.group(): Cats are smarter than dogs
matchObj.group(1): Cats
matchObj.group(2):  smarter than dogs
matchObj.group(3):

那么这是否就意味着?的默认值很可能是0次,那?这个符号到底有什么用呢

仔细想来这个说法并不是很严谨。尝试使用单独的.?组合可以看到这个组合可以用于提取

单个不知道是否存在的字符,而如下代码

import re

line='Cats are smarter than dogs'
matchObj=re.match(r'(.*) are(.*)?',line)

if matchObj:
 print("matchObj.group():",matchObj.group())
 print("matchObj.group(1):", matchObj.group(1))
 print("matchObj.group(2):", matchObj.group(2))

也能在组别2中正常提取到are之后的字符内容,但稍微改动一下将?放到第二个括号内,

就什么也提取不到,同时导致group(0)中匹配的字符到Cats are就截止了(也就是第二个括号匹配失败)。

令人感到奇怪的是,如果将上面的代码改成

import re

line='Cats are smarter than dogs'
matchObj=re.match(r'(.*) are (.*)+',line)

if matchObj:
 print("matchObj.group():",matchObj.group())
 print("matchObj.group(1):", matchObj.group(1))
 print("matchObj.group(2):", matchObj.group(2))

也就是仅仅将?改为+,虽然能成功匹配整个line但group(2)中没有内容,

如果把+放到第二个括号中就会产生报错,匹配失败。

那么是否可以认为.*?这三个符号连用只是一个不规范的操作,但由于?的特殊性所以没有报错反而匹配成功了呢?

具体的可能要研究代码本身的机理了,暂且搁置。还有一个问题就是如何达到样例本身想要的,用第二个括号提取单个单词的目的。

如果单单考虑这个例子的话,把原本第二个括号中的?换成r就可以了,也就是如下代码:

import re

line='Cats are smarter than dogs'
matchObj=re.match(r'(.*) are (.*r).*',line)

if matchObj:
 print("matchObj.group():",matchObj.group())
 print("matchObj.group(1):", matchObj.group(1))
 print("matchObj.group(2):", matchObj.group(2))
 #print("matchObj.group(3):", matchObj.group(3))
else:
 print('No match!\n')

为了泛用性尝试了一下把r改成‘ '但是得到的结果是‘smarter than '。于是尝试把.换成表示任意字母的

[a-zA-Z],成功提取出了单个smarter,代码如下:

import re

line='Cats are smarter than dogs'
matchObj=re.match(r'(.*) are ([a-zA-Z]* ).*',line)

if matchObj:
 print("matchObj.group():",matchObj.group())
 print("matchObj.group(1):", matchObj.group(1))
 print("matchObj.group(2):", matchObj.group(2))
 #print("matchObj.group(3):", matchObj.group(3))
else:
 print('No match!\n')

到此这篇关于python re.match()用法相关示例的文章就介绍到这了,更多相关python re.match()内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python 远程统计文件代码分享
May 14 Python
详解Django+Uwsgi+Nginx 实现生产环境部署
Nov 06 Python
python实现石头剪刀布程序
Jan 20 Python
python pytest进阶之xunit fixture详解
Jun 27 Python
python全栈知识点总结
Jul 01 Python
Python中six模块基础用法
Dec 08 Python
Python pyautogui模块实现鼠标键盘自动化方法详解
Feb 17 Python
Python collections.defaultdict模块用法详解
Jun 18 Python
python使用建议与技巧分享(二)
Aug 17 Python
基于Python-turtle库绘制路飞的草帽骷髅旗、美国队长的盾牌、高达的源码
Feb 18 Python
python实现剪贴板的操作
Jul 01 Python
python读取并查看npz/npy文件数据以及数据显示方法
Apr 14 Python
selenium+python实现基本自动化测试的示例代码
Jan 27 #Python
Python开发.exe小工具的详细步骤
Jan 27 #Python
Python中正则表达式对单个字符,多个字符和匹配边界等使用
Jan 27 #Python
python正则表达式re.match()匹配多个字符方法的实现
Jan 27 #Python
python工具快速为音视频自动生成字幕(使用说明)
Jan 27 #Python
详解appium自动化测试工具(monitor、uiautomatorviewer)
Jan 27 #Python
Python利用socket模块开发简单的端口扫描工具的实现
Jan 27 #Python
You might like
Excel数据导入Mysql数据库的实现代码
2008/06/05 PHP
逆序二维数组插入一元素的php代码
2012/06/08 PHP
如何使用PHP获取指定日期所在月的开始日期与结束日期
2013/08/01 PHP
PHP中FTP相关函数小结
2016/07/15 PHP
通过修改Laravel Auth使用salt和password进行认证用户详解
2017/08/17 PHP
验证手机号码的JS方法分享
2013/09/10 Javascript
浅谈javascript函数式编程
2015/09/06 Javascript
JavaScript与java语言有什么不同
2016/09/22 Javascript
Angular.js中下拉框实现渲染html的方法
2017/06/18 Javascript
基于Vue实现拖拽效果
2018/04/27 Javascript
npm 下载指定版本的组件方法
2018/05/17 Javascript
vue中子组件的methods中获取到props中的值方法
2018/08/27 Javascript
详解VScode编辑器vue环境搭建所遇问题解决方案
2019/04/26 Javascript
5分钟快速看懂ES6中的反射与代理
2019/12/19 Javascript
vue flex 布局实现div均分自动换行的示例代码
2020/08/05 Javascript
js实现滚动条自动滚动
2020/12/13 Javascript
Python采用raw_input读取输入值的方法
2014/08/18 Python
Linux下编译安装MySQL-Python教程
2015/02/02 Python
Python多线程编程(三):threading.Thread类的重要函数和方法
2015/04/05 Python
Django中URLconf和include()的协同工作方法
2015/07/20 Python
浅谈python内置变量-reversed(seq)
2017/06/21 Python
详解PyCharm配置Anaconda的艰难心路历程
2018/08/13 Python
在自动化中用python实现键盘操作的方法详解
2019/07/19 Python
Python笔试面试题小结
2019/09/07 Python
使用python实现飞机大战游戏
2020/03/23 Python
Python Selenium自动化获取页面信息的方法
2020/08/31 Python
Scrapy项目实战之爬取某社区用户详情
2020/09/17 Python
详解pycharm自动import所需的库的操作方法
2020/11/30 Python
html5+css3气泡组件的实现
2014/11/21 HTML / CSS
英国国家美术馆商店:National Gallery
2019/05/01 全球购物
英国汽车零件购物网站:GSF Car Parts
2019/05/23 全球购物
《三顾茅庐》教学反思
2014/04/10 职场文书
山东省召开党的群众路线教育实践活动总结大会新闻稿
2014/10/21 职场文书
平安家庭事迹材料
2014/12/20 职场文书
三好学生竞选稿
2015/11/21 职场文书
Vue中foreach数组与js中遍历数组的写法说明
2021/06/05 Vue.js