2016-02-24 35 views

回答

2

通過刪除那些註釋行來獲得所需的輸出。

re.sub(r'(?s)<!--.*?-->', '', html) 

實施例:

>>> html = '''<html> 
<head> 
<p>some code</p> 
<!-- 
<link href='www.xxx.com'> 
--> 
<head> 
<body> 
<p>some more code</p> 
</body></html>''' 
>>> print(re.sub(r'(?s)\s*<!--.*?-->', '', html)) 
<html> 
<head> 
<p>some code</p> 
<head> 
<body> 
<p>some more code</p> 
</body></html> 
+0

什麼是''(S')? – Shafizadeh

+1

使您的正則表達式中的點符合換行符。 –

+0

你是怎麼在沒有在你的評論中寫這個'@ Shafizade'而寫我的用戶名的? – Shafizadeh

相關問題