第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

貪婪和懶惰量詞。使用 HTML 標(biāo)簽進(jìn)行測(cè)試

貪婪和懶惰量詞。使用 HTML 標(biāo)簽進(jìn)行測(cè)試

達(dá)令說 2023-10-10 10:35:00
輸入是<p>The very <em>first</em> task is to find the beginning of a paragraph.</p><p>Then you have to find the end of the paragraph</p>預(yù)期的第一個(gè)輸出是(因?yàn)槲沂褂秘澙妨吭~)<p>The very <em>first</em> task is to find the beginning of a paragraph.</p><p>Then you have to find the end of the paragraph</p>用于貪婪的代碼如下text = '''<p>The very <em>first</em> task is to find the beginning of a paragraph.</p><p>Then you have to find the end of the paragraph</p>'''pattern=re.compile(r'\<p\>.*\<\/p\>')data1=pattern.match(text,re.MULTILINE)print('data1:- ',data1,'\n')預(yù)期的第二個(gè)輸出是(因?yàn)槲沂褂玫氖嵌栊粤吭~)<p>The very <em>first</em> task is to find the beginning of a paragraph.</p>用于懶惰的代碼如下text = '''<p>The very <em>first</em> task is to find the beginning of a paragraph.</p><p>Then you have to find the end of the paragraph</p>'''#pattern=re.compile(r'\<p\>.*?\<\/p\>')pattern=re.compile(r'<p>.*?</p>')data1=pattern.match(text,re.MULTILINE)print('data1:- ',data1,'\n')我得到的實(shí)際輸出都是 None
查看完整描述

1 回答

?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

你有幾個(gè)問題。首先,使用 時(shí)Pattern.match,第二個(gè)和第三個(gè)參數(shù)是位置參數(shù),而不是標(biāo)志。需要將標(biāo)志指定為re.compile。其次,您應(yīng)該使用re.DOTALL.匹配換行符,而不是re.MULTILINE.?最后 -match堅(jiān)持匹配發(fā)生在字符串的開頭(在您的情況下是換行符),因此它不會(huì)匹配。您可能想改用Pattern.search。這適用于您的示例輸入:


pattern=re.compile(r'<p>.*</p>', re.DOTALL)

data1=pattern.search(text)

print('data1:- ',data1.group(0),'\n')

輸出:


data1:-? <p>

The very <em>first</em> task is to find the beginning of a paragraph.

</p>

<p>

Then you have to find the end of the paragraph

</p>?

單場(chǎng)比賽:


pattern=re.compile(r'<p>.*?</p>', re.DOTALL)

data1=pattern.search(text)

print('data1:- ',data1.group(0),'\n')

輸出:


data1:-? <p>

The very <em>first</em> task is to find the beginning of a paragraph.

</p>?

另請(qǐng)注意/, ,<和>在正則表達(dá)式中沒有特殊含義,不需要轉(zhuǎn)義。我已經(jīng)在上面的代碼中刪除了它。


查看完整回答
反對(duì) 回復(fù) 2023-10-10
  • 1 回答
  • 0 關(guān)注
  • 120 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)