温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

re.match()方法怎么在python中使用

发布时间:2021-01-27 14:10:26 来源:亿速云 阅读:165 作者:Leah 栏目:开发技术

re.match()方法怎么在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')

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI