[英]How to remove specific string between two substrings from given string in python?
我正在嘗試刪除給定字符串中的某些文本。 因此問題如下。 我有一個字符串。 像這樣說HTML代碼。
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
我希望代碼刪除所有與CSS相關的代碼。 即字符串現在應如下所示:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
我已經嘗試過使用python中的此功能:
def css_remover(text):
m = re.findall('<style>(.*)</style>$', text,re.DOTALL)
if m:
for eachText in text.split(" "):
for eachM in m:
if eachM in eachText:
text=text.replace(eachText,"")
print(text)
但這是行不通的。 我希望函數處理空格,換行符,以便刪除<style> </style>
標記之間的所有內容。 另外,我希望標簽上沒有附加任何單詞,它們不會受到影響。 像hello<style> klasjdklasd </style>>
應該產生hello>
您輸入$
表示字符串的結尾。 嘗試這個:
x = re.sub('<style>.*?</style>', '', text, flags=re.DOTALL)
print(x)
您可以查看該網站 ,有一個不錯的正則表達式演示。
一點注意 :我對CSS並不是很熟悉,因此如果嵌套了<style>
標簽,可能會出現問題。
特別注意?
RegExp表達式的<style>(.*?)</style>
部分中的字符,以免過於“貪婪”。 否則,在下面的示例中,它還將刪除<title>
HTML標記。
import re
text = """
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
<title>Test</title>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
"""
regex = re.compile(r' *<style>(.*?)</style> *\n?', re.DOTALL|re.MULTILINE)
text = regex.sub('', text, 0)
print (text == """
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
""")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.