c# - 正则表达式 - 删除跨越多个换行符的 HTML 注释

标签 c# regex vb.net replace

我正在使用这个脚本:

http://www.codeproject.com/Articles/11902/Convert-HTML-to-Plain-Text

将一些 outlook HTML 转换为纯文本。

它几乎可以工作,唯一留下的是 outlook 放置在 html 注释标签中的 CSS <!-- -->除了<style>标签(已删除)

原文为:

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family:"Calibri","sans-serif";
    mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
    {mso-style-priority:99;
    color:blue;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {mso-style-priority:99;
    color:purple;
    text-decoration:underline;}
span.EmailStyle17
    {mso-style-type:personal-compose;
    font-family:"Calibri","sans-serif";
    color:windowtext;}
.MsoChpDefault
    {mso-style-type:export-only;
    font-family:"Calibri","sans-serif";
    mso-fareast-language:EN-US;}
@page WordSection1
    {size:612.0pt 792.0pt;
    margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
    {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">tesst<o:p></o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:dimgray;mso-fareast-language:EN-GB">JOE BLOGS</span></b><span style="font-size:10.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:dimgray;mso-fareast-language:EN-GB">
</div>
</body>
</html>

这是结果文本:(请注意 HTML 注释尚未删除)

<!--
/* Font Definitions */
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin:0cm;
    margin-bottom:.0001pt;
    font-size:11.0pt;
    font-family:"Calibri","sans-serif";
    mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
    {mso-style-priority:99;
    color:blue;
    text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
    {mso-style-priority:99;
    color:purple;
    text-decoration:underline;}
span.EmailStyle17
    {mso-style-type:personal-compose;
    font-family:"Calibri","sans-serif";
    color:windowtext;}
.MsoChpDefault
    {mso-style-type:export-only;
    font-family:"Calibri","sans-serif";
    mso-fareast-language:EN-US;}
@page WordSection1
    {size:612.0pt 792.0pt;
    margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
    {page:WordSection1;}
-->

tesst
&nbsp;
JOE BLOGS

我已经尝试使用额外的替换来调整 StripHTML() 函数 - 但这些也不起作用。

result = System.Text.RegularExpressions.Regex.Replace(result, "(<!--).*?(-->)", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "<!--*-->", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)

请帮忙 - 这是一个 2 分钟的工作,我从午餐后一直坚持 facedesk

干杯

编辑 1:也尝试了以下 - 仍然不开心

result = System.Text.RegularExpressions.Regex.Replace(result, "<!--.*-->", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
result = System.Text.RegularExpressions.Regex.Replace(result, "<!--.*?-->", String.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)

编辑 2: 我注意到这个问题有很多观点,任何阅读此内容的人在采用 regExp 方法时一定要三思,相反我建议使用 Lynx(基于 OpenSource 文本的浏览器)来转换HTML转纯文本,我问过类似的问题here并且我根据答案在编辑中提供示例代码,这些答案应该让您开始在 .net 应用程序中使用 lynx.exe。这是我们最终使用的方法,此后没有出现任何问题。

最佳答案

你的第二个正则表达式有三个原因:

  • 你需要使用.来匹配任何字符。
  • * 是贪心的。您希望 *? 延迟匹配。
  • 您需要 RegexOptions.Singleline

试试这个:

result = Regex.Replace(result, "<!--.*?-->", "", RegexOptions.Singleline);

我强烈建议您不要使用正则表达式来解析 HTML。如果您改为使用 HTML Agility Pack,您将避免整个世界的痛苦。 .

关于c# - 正则表达式 - 删除跨越多个换行符的 HTML 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10656123/

相关文章:

regex - 在applescript 中是否有类似于regEx 的东西,如果没有,还有什么选择?

mysql - 在 MySQL SELECT 语句中使用正则表达式的正确格式(不在 WHERE 部分内)

javascript - 使用正则表达式选择元素 jQuery

javascript - 在 Ajax 中获取变量中的复选框值

vb.net - 使用 Gmail 和 C#/VB.Net 发送电子邮件不再有效

c# - 如何允许 .NET Core 控制台应用程序通过 Windows 防火墙进行 FTP 连接?

c# - VS 代码 : Extract interface in C#

asp.net - 如何迭代自定义 vb.net 对象的每个属性?

c# - 如何将 WriteLine 拆分为多行?

c# - 如何将字符串添加到列表<String>? List<String> 是模型中的模型