最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

達(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ù)期的第一個輸出是(因為我使用貪婪量詞)<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ù)期的第二個輸出是(因為我使用的是惰性量詞)<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')我得到的實際輸出都是 None
查看完整描述

1 回答

?
蝴蝶刀刀

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

你有幾個問題。首先,使用 時Pattern.match,第二個和第三個參數(shù)是位置參數(shù),而不是標(biāo)志。需要將標(biāo)志指定為re.compile。其次,您應(yīng)該使用re.DOTALL.匹配換行符,而不是re.MULTILINE.?最后 -match堅持匹配發(fā)生在字符串的開頭(在您的情況下是換行符),因此它不會匹配。您可能想改用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>?

單場比賽:


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>?

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


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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