學(xué)習(xí)python爬蟲(chóng)時(shí)遇到了一個(gè)問(wèn)題,書(shū)上有示例如下:
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')
書(shū)上的期望輸出是:
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):
于是開(kāi)始想辦法徹底搞清楚這個(gè)差別的原因所在。
首先要讀懂這幾行代碼,而這一行代碼的關(guān)鍵在于這一句:
matchObj=re.match(r'(.*)are(.*?).*',line)
匹配的正則表達(dá)式是
(.*)are(.*?).*
前面的r表示的是匹配的字符不進(jìn)行轉(zhuǎn)義,而要匹配的字符串是line,也就是
Cats are smarter than dogs
后面使用group(num),個(gè)人理解是,按照正則表達(dá)式中的括號(hào)數(shù)可以捕獲得到對(duì)應(yīng)數(shù)量的捕獲組,而調(diào)用group(num)就可以得到對(duì)應(yīng)捕獲組的內(nèi)容,
其中g(shù)roup(0)表示的是匹配的整個(gè)表達(dá)式的字符串,在本例中就是‘Cats are smarter than dogs'。
參照網(wǎng)上可以搜到的符號(hào)的作用:
.匹配除換行符以外的任意字符
*重復(fù)之前的字符零次或更多次
?重復(fù)之前的字符零次或一次
那么第一個(gè)括號(hào)的內(nèi)容,應(yīng)當(dāng)就是匹配要匹配的字符串中are之前的所有字符(除換行符),
而第二個(gè)括號(hào)的內(nèi)容應(yīng)當(dāng)是匹配are之后的內(nèi)容,但具體想指代什么卻顯得有些不明確。
不明確的點(diǎn)就在于*和?這兩個(gè)符號(hào)的連用,根據(jù)優(yōu)先級(jí)這兩個(gè)符號(hào)是同一優(yōu)先級(jí)的,那么應(yīng)當(dāng)按照順序生效,那么如此翻譯的話,這一語(yǔ)句匹配的就是長(zhǎng)度為0到無(wú)限大的任意字符串,為了探清此時(shí)
程序判斷的具體內(nèi)容,我們給匹配字符串末尾的.*也加上括號(hào)以提取其內(nèi)容,而后在輸出部分加上對(duì)應(yīng)語(yǔ)句:
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')
得到的結(jié)果是:
matchObj.group(): Cats are smarter than dogs
matchObj.group(1): Cats
matchObj.group(2):
matchObj.group(3): smarter than dogs
可見(jiàn)第二個(gè)括號(hào)里的內(nèi)容被默認(rèn)為空了,然后刪去那個(gè)?,可以看到結(jié)果變成:
matchObj.group(): Cats are smarter than dogs
matchObj.group(1): Cats
matchObj.group(2): smarter than dogs
matchObj.group(3):
那么這是否就意味著?的默認(rèn)值很可能是0次,那?這個(gè)符號(hào)到底有什么用呢
仔細(xì)想來(lái)這個(gè)說(shuō)法并不是很嚴(yán)謹(jǐn)。嘗試使用單獨(dú)的.?組合可以看到這個(gè)組合可以用于提取
單個(gè)不知道是否存在的字符,而如下代碼
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之后的字符內(nèi)容,但稍微改動(dòng)一下將?放到第二個(gè)括號(hào)內(nèi),
就什么也提取不到,同時(shí)導(dǎo)致group(0)中匹配的字符到Cats are就截止了(也就是第二個(gè)括號(hào)匹配失?。?。
令人感到奇怪的是,如果將上面的代碼改成
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))
也就是僅僅將?改為+,雖然能成功匹配整個(gè)line但group(2)中沒(méi)有內(nèi)容,
如果把+放到第二個(gè)括號(hào)中就會(huì)產(chǎn)生報(bào)錯(cuò),匹配失敗。
那么是否可以認(rèn)為.*?這三個(gè)符號(hào)連用只是一個(gè)不規(guī)范的操作,但由于?的特殊性所以沒(méi)有報(bào)錯(cuò)反而匹配成功了呢?
具體的可能要研究代碼本身的機(jī)理了,暫且擱置。還有一個(gè)問(wèn)題就是如何達(dá)到樣例本身想要的,用第二個(gè)括號(hào)提取單個(gè)單詞的目的。
如果單單考慮這個(gè)例子的話,把原本第二個(gè)括號(hào)中的?換成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改成‘ '但是得到的結(jié)果是‘smarter than '。于是嘗試把.換成表示任意字母的
[a-zA-Z],成功提取出了單個(gè)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')
到此這篇關(guān)于python re.match()用法相關(guān)示例的文章就介紹到這了,更多相關(guān)python re.match()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python正則表達(dá)式re.match()匹配多個(gè)字符方法的實(shí)現(xiàn)
- 淺談Python中re.match()和re.search()的使用及區(qū)別