1 回答

TA貢獻1801條經驗 獲得超8個贊
你有幾個問題。首先,使用 時Pattern.match
,第二個和第三個參數是位置參數,而不是標志。需要將標志指定為re.compile
。其次,您應該使用re.DOTALL
來.
匹配換行符,而不是re.MULTILINE
.?最后 -match
堅持匹配發生在字符串的開頭(在您的情況下是換行符),因此它不會匹配。您可能想改用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>?
另請注意/, ,<和>在正則表達式中沒有特殊含義,不需要轉義。我已經在上面的代碼中刪除了它。
- 1 回答
- 0 關注
- 122 瀏覽
添加回答
舉報