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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

虚在君   2021-01-27 我要评论

学习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')

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们