正则表达式匹配 XML 注释

正则表达式匹配 XML 注释

我正在寻找一个正则表达式来匹配 XML 文档中的注释:

<root>
<!-- 
    match this 
-->
<but>not this</but>
<!--
     and also this
-->
</root>

我已经尝试过<!--[^(-->)]*-->,它只匹配单行注释,并且<!--[\s\S\n]*-->也匹配非注释节点。

答案1

您正在寻找的正则表达式是:

<!--[\s\S\n]*?-->

解释:

 <!--               All comments must begin with this
     [\s\S\n]       Any character (. doesn't allow newlines)
             *      0 or more of the previous thing ([\s\S\n])
              ?     As few of the previous thing as possible while still matching
               -->  All comments must end with this

但是如果你在评论里面再加一条评论的话就会出现问题:

<!-- Documentation
This program documents itself using comments of the type <!-- -->
-->

粗体突出显示表示匹配

相关内容