我如何找到评论标签之间的所有换行符和字符?

How I can find all of newline and characters between comments tags?

我如何匹配以下代码。例如我有:

<!--[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)*<!.*-->

我只需要 matchregular expression,然后替换它。我不需要保留任何标签。但是我需要从 <!--[if !mso]> 开始查找并以 <![endif]--> 结束查找。

使用 [\s\S]*? 对任何字符进行零次或多次非贪婪匹配。

<!--.*?>([\s\S]*?)<!.*?-->

(?s)<!--.*?>(.*?)<!.*?-->

(?s) DOTALL 修饰符,使正则表达式中的点也匹配换行符 (\n, \r)

<!--.*?>((?:.|\n)*?)<!.*?-->

你的正则表达式是 fine.Just 使所有 * 贪婪量词非 greedy.See 演示。

https://regex101.com/r/tX2bH4/38

试试这个:

<!--.*>([\s\S]*)<!\[endif\]-->

演示:https://regex101.com/r/aL9bW9/1

试试这个:

(?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  (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  (NOTE: because you are using a
               quantifier on this capture, only the LAST
               repetition of the captured pattern will be
               stored in )
----------------------------------------------------------
  -->          '-->'