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

56e452ce1dcf74ec629cb6798e40082a.png

学习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!

")

书上的期望输出是:

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!

")

得到的结果是:

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!

")

为了泛用性尝试了一下把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!

")

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值