python 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')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM