使用正则表达式的文字删除CSS我有一个字符串,如:从在Python 3
"<p>
<style type=""text/css"">
P { margin-bottom: 0.08in; direction: ltr; widows: 2; orphans: 2; }A:link { color: rgb(0, 0, 255); } </style>
</p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
<font face=""Trebuchet MS, Arial, Verdana, sans-serif""><span style=""font-size: 12px; background-color: rgb(238, 238, 238);"">blablabla. </span></font></p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
<font face=""Trebuchet MS, Arial, Verdana, sans-serif""><span style=""font-size: 12px; background-color: rgb(238, 238, 238);"">tjatjatja</span></font><span style=""font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);"">tjetjetje</span><span style=""font-size: 12px; font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; background-color: rgb(238, 238, 238);"">.</span></p>
<p style=""font-variant: normal; font-style: normal; font-weight: normal"">
<span style=""font-family: 'Trebuchet MS', Arial, Verdana, sans-serif; font-size: 12px; background-color: rgb(238, 238, 238);"">huehuehue</span></p>
"
我想删除第一个样式标签和它的内容。我有这样一个正则表达式:
([\s\S]*)<style type=""text\/css"">[\s\S]+<\/style>([\s\S]*)
刚刚的第一个样式标签匹配,但是当我尝试与删除它在python:
re.sub(r'([\s\S]*)<style type=""text/css"">[\s\S]*</style>([\s\S]*)', r'\1\2', cell_text, flags=re.M)
这是行不通的。我认为这要么是与团体打交道,要么是以多线的形式进行。有任何想法吗?
如果可以使用更多'style'标签,至少必须使''\ s \ S] *'非贪婪('[\ s \ S] *?')。 – ClasG
And ...我不是Python专家,但你的正则表达式有单引号 - 为什么2'“'在里面?我猜字符串有2,因为这是你如何在Python中转义引号,但不应该是必须在一个单引号字符串中,或者...? – ClasG
不知道为什么示例数据包含引号。为了对抗它,我使用了包含正则表达式的原始字符串的单引号 – tjarles