我如何匹配以下代码。例如,我有:
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
我需要:
<!--.*>(.|\n)*<!.*-->
我只需要match
regular expression
,然后替换它。我不需要保留任何代码。但是我需要从<!--[if !mso]>
开始查找并找到<![endif]-->
的结尾。
答案 0 :(得分:1)
使用[\s\S]*?
对任意字符进行零次或多次非贪婪匹配。
<!--.*?>([\s\S]*?)<!.*?-->
OR
(?s)<!--.*?>(.*?)<!.*?-->
(?s)
DOTALL修饰符,它使正则表达式中的点也与换行符匹配(\n
,\r
)
答案 1 :(得分:0)
答案 2 :(得分:0)
答案 3 :(得分:0)
试试这个:
(?s)<!--((?!-->).)*-->
正则表达式中每个项目的说明:
NODE EXPLANATION
----------------------------------------------------------
(?s) set flags for this block (with . matching
\n) (case-sensitive) (with ^ and $
matching normally) (matching whitespace
and # normally)
----------------------------------------------------------
<!-- '<!--'
----------------------------------------------------------
( group and capture to \1 (0 or more times
(matching the most amount possible)):
----------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------
--> '-->'
----------------------------------------------------------
) end of look-ahead
----------------------------------------------------------
. any character
----------------------------------------------------------
)* end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
----------------------------------------------------------
--> '-->'