婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python re.match()用法相關(guān)示例

python re.match()用法相關(guān)示例

熱門標(biāo)簽:滴滴地圖標(biāo)注公司 天津塘沽區(qū)地圖標(biāo)注 杭州房產(chǎn)地圖標(biāo)注 甘肅高頻外呼系統(tǒng) 江門智能電話機(jī)器人 400電話在線如何申請 如何申請400電話代理 地圖標(biāo)注可以遠(yuǎn)程操作嗎 智能電話機(jī)器人調(diào)研

學(xué)習(xí)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):

于是開始想辦法徹底搞清楚這個差別的原因所在。

首先要讀懂這幾行代碼,而這一行代碼的關(guān)鍵在于這一句:

matchObj=re.match(r'(.*)are(.*?).*',line)

匹配的正則表達(dá)式是

(.*)are(.*?).*
前面的r表示的是匹配的字符不進(jìn)行轉(zhuǎn)義,而要匹配的字符串是line,也就是
Cats are smarter than dogs
后面使用group(num),個人理解是,按照正則表達(dá)式中的括號數(shù)可以捕獲得到對應(yīng)數(shù)量的捕獲組,而調(diào)用group(num)就可以得到對應(yīng)捕獲組的內(nèi)容,
其中g(shù)roup(0)表示的是匹配的整個表達(dá)式的字符串,在本例中就是‘Cats are smarter than dogs'。
參照網(wǎng)上可以搜到的符號的作用:
.匹配除換行符以外的任意字符
*重復(fù)之前的字符零次或更多次
?重復(fù)之前的字符零次或一次
那么第一個括號的內(nèi)容,應(yīng)當(dāng)就是匹配要匹配的字符串中are之前的所有字符(除換行符),
而第二個括號的內(nèi)容應(yīng)當(dāng)是匹配are之后的內(nèi)容,但具體想指代什么卻顯得有些不明確。
不明確的點就在于*和?這兩個符號的連用,根據(jù)優(yōu)先級這兩個符號是同一優(yōu)先級的,那么應(yīng)當(dāng)按照順序生效,那么如此翻譯的話,這一語句匹配的就是長度為0到無限大的任意字符串,為了探清此時
程序判斷的具體內(nèi)容,我們給匹配字符串末尾的.*也加上括號以提取其內(nèi)容,而后在輸出部分加上對應(yīng)語句:

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

可見第二個括號里的內(nèi)容被默認(rèn)為空了,然后刪去那個?,可以看到結(jié)果變成:

matchObj.group(): Cats are smarter than dogs
matchObj.group(1): Cats
matchObj.group(2):  smarter than dogs
matchObj.group(3):

那么這是否就意味著?的默認(rèn)值很可能是0次,那?這個符號到底有什么用呢

仔細(xì)想來這個說法并不是很嚴(yán)謹(jǐn)。嘗試使用單獨的.?組合可以看到這個組合可以用于提取

單個不知道是否存在的字符,而如下代碼

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)容,但稍微改動一下將?放到第二個括號內(nèi),

就什么也提取不到,同時導(dǎo)致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)中沒有內(nèi)容,

如果把+放到第二個括號中就會產(chǎn)生報錯,匹配失敗。

那么是否可以認(rèn)為.*?這三個符號連用只是一個不規(guī)范的操作,但由于?的特殊性所以沒有報錯反而匹配成功了呢?

具體的可能要研究代碼本身的機(jī)理了,暫且擱置。還有一個問題就是如何達(dá)到樣例本身想要的,用第二個括號提取單個單詞的目的。

如果單單考慮這個例子的話,把原本第二個括號中的?換成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],成功提取出了單個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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python正則表達(dá)式re.match()匹配多個字符方法的實現(xiàn)
  • 淺談Python中re.match()和re.search()的使用及區(qū)別

標(biāo)簽:河池 漢中 德宏 臨汾 長春 重慶 廊坊 東莞

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python re.match()用法相關(guān)示例》,本文關(guān)鍵詞  python,re.match,用法,相關(guān),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python re.match()用法相關(guān)示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于python re.match()用法相關(guān)示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 天长市| 大丰市| 万源市| 宜阳县| 海口市| 宁远县| 江都市| 凌海市| 马山县| 灵璧县| 十堰市| 闸北区| 崇信县| 神木县| 巴南区| 朝阳市| 建阳市| 双流县| 兴和县| 新龙县| 手机| 株洲市| 阿巴嘎旗| 丽江市| 凤山县| 隆昌县| 西乌| 房产| 大英县| 乐安县| 高碑店市| 汪清县| 冀州市| 湛江市| 思南县| 旺苍县| 股票| 咸阳市| 洞头县| 连州市| 蓝田县|