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)在上面的代碼中刪除了它。
- 1 回答
- 0 關(guān)注
- 120 瀏覽
添加回答
舉報(bào)