python re.match()用法相關示例
學習python爬蟲時遇到了一個問題,書上有示例如下:
import reline=’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 dogsmatchObj.group(1): Cats matchObj.group(2):smarter
但是我在電腦上跑了一遍得到的輸出卻是:
matchObj.group(): Cats are smarter than dogsmatchObj.group(1): Cats matchObj.group(2):
于是開始想辦法徹底搞清楚這個差別的原因所在。
首先要讀懂這幾行代碼,而這一行代碼的關鍵在于這一句:
matchObj=re.match(r’(.*)are(.*?).*’,line)
匹配的正則表達式是
(.*)are(.*?).*前面的r表示的是匹配的字符不進行轉義,而要匹配的字符串是line,也就是Cats are smarter than dogs后面使用group(num),個人理解是,按照正則表達式中的括號數(shù)可以捕獲得到對應數(shù)量的捕獲組,而調用group(num)就可以得到對應捕獲組的內容,其中group(0)表示的是匹配的整個表達式的字符串,在本例中就是‘Cats are smarter than dogs’。參照網(wǎng)上可以搜到的符號的作用:.匹配除換行符以外的任意字符*重復之前的字符零次或更多次?重復之前的字符零次或一次那么第一個括號的內容,應當就是匹配要匹配的字符串中are之前的所有字符(除換行符),而第二個括號的內容應當是匹配are之后的內容,但具體想指代什么卻顯得有些不明確。不明確的點就在于*和?這兩個符號的連用,根據(jù)優(yōu)先級這兩個符號是同一優(yōu)先級的,那么應當按照順序生效,那么如此翻譯的話,這一語句匹配的就是長度為0到無限大的任意字符串,為了探清此時程序判斷的具體內容,我們給匹配字符串末尾的.*也加上括號以提取其內容,而后在輸出部分加上對應語句:
import reline=’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 dogsmatchObj.group(1): Cats matchObj.group(2): matchObj.group(3): smarter than dogs
可見第二個括號里的內容被默認為空了,然后刪去那個?,可以看到結果變成:
matchObj.group(): Cats are smarter than dogsmatchObj.group(1): Cats matchObj.group(2): smarter than dogsmatchObj.group(3):
那么這是否就意味著?的默認值很可能是0次,那?這個符號到底有什么用呢
仔細想來這個說法并不是很嚴謹。嘗試使用單獨的.?組合可以看到這個組合可以用于提取
單個不知道是否存在的字符,而如下代碼
import reline=’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 reline=’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)中沒有內容,
如果把+放到第二個括號中就會產(chǎn)生報錯,匹配失敗。
那么是否可以認為.*?這三個符號連用只是一個不規(guī)范的操作,但由于?的特殊性所以沒有報錯反而匹配成功了呢?
具體的可能要研究代碼本身的機理了,暫且擱置。還有一個問題就是如何達到樣例本身想要的,用第二個括號提取單個單詞的目的。
如果單單考慮這個例子的話,把原本第二個括號中的?換成r就可以了,也就是如下代碼:
import reline=’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 reline=’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’)
到此這篇關于python re.match()用法相關示例的文章就介紹到這了,更多相關python re.match()內容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. 一個 2 年 Android 開發(fā)者的 18 條忠告2. Vue實現(xiàn)仿iPhone懸浮球的示例代碼3. js select支持手動輸入功能實現(xiàn)代碼4. vue-drag-chart 拖動/縮放圖表組件的實例代碼5. 什么是Python變量作用域6. Spring的異常重試框架Spring Retry簡單配置操作7. Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關的進程8. PHP正則表達式函數(shù)preg_replace用法實例分析9. Android studio 解決logcat無過濾工具欄的操作10. vue使用moment如何將時間戳轉為標準日期時間格式
