C#中如何删除与正则表达式XML / HTML注释 [英] C# How to delete XML/HTML comments with regular expression

查看:347
本文介绍了C#中如何删除与正则表达式XML / HTML注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该片段下方不为我工作。

 片段= Regex.Replace(片段,<  - !?*  - >中,的String.Empty ,RegexOptions.Multiline); 


解决方案

将其更改为的 RegExOptions.Singleline 来代替,而它会工作正好。
当不是在单线模式,点匹配任何字符,除了的换行符。



注意 SINGLELINE 不是相互排斥的。他们这样做两回事。引述MSDN:




多行模式即可。改变
的含义^和$所以它们匹配开头
和结束,分别任意行,
的,不只是开始和$ B $的末端b的整个字符串。



单行模式即可。更改
点的含义(。),因此它
每一个字符(而不是
除了\\\
每个字符)相匹配。




其他人已经提出了HTML敏捷性包。我只是觉得你应该有,为什么你的正则表达式是行不通的解释:)


The fragment below doesn't work for me.

fragment = Regex.Replace(fragment, "<!--.*?-->", String.Empty , RegexOptions.Multiline  );

解决方案

Change it to RegExOptions.Singleline instead and it'll work just fine. When not in Singleline mode, the dot matches any character, except newline.

Note that Singleline and Multiline are not mutually exclusive. They do two separate things. To quote MSDN:

Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.

Single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).

Other people have already suggested the HTML Agility Pack. I just felt you should have an explanation on why your Regex wouldn't work :)

这篇关于C#中如何删除与正则表达式XML / HTML注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆