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

5

我正在使用这个脚本:

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

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

它几乎可以工作,唯一留下的是Outlook放在HTML注释标签<-- -->以及<style>标签(已被删除)中的CSS。

这是原始文本:

<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)

请帮忙 - 这是一个只需两分钟的工作,但我从午餐时间开始就一直卡住了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(开源文本浏览器)将HTML转换为纯文本。我在这里提出了类似的问题here并且根据答案提供了示例代码来让你在.net应用程序中开始使用lynx.exe。这是我们最终采用的方法,自从使用以来就没有出现过任何问题。


1
使用正则表达式来解析 HTML 并不是一个好主意。可以考虑使用 Html Agility Pack 代替。 - Mark Byers
1
可能相关:https://dev59.com/X3I-5IYBdhLWcg3wq6do - Matthew
我在HTML Agility Pack中找不到converttoplaintext()方法,它们已经移除了吗? - HeavenCore
@HeavenCore:它仍然存在,可以在示例中找到:http://htmlagilitypack.codeplex.com/SourceControl/changeset/view/94773#1336937 - Mark Byers
2个回答

7
以下是需要翻译的内容:

你需要第二个正则表达式,原因如下:

  • 你需要使用 . 匹配任何字符。
  • * 是贪婪的。你需要使用 *? 进行懒惰匹配。
  • 你需要使用 RegexOptions.Singleline

尝试这个:

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

我强烈建议您不要使用正则表达式来解析HTML。如果你改用HTML Agility Pack,将会节省很多时间和精力。


抱歉,马克,看到编辑1了 - 这两个都没有删除评论部分。 - HeavenCore
嗨,马克,<!--.*?--> + Singleline 也不起作用,但我已经采纳了建议,放弃了正则表达式选项,现在正在使用HTML Agilty Pack解决方案,并且如果遇到任何问题/找到解决方案,我会更新我的问题。 - HeavenCore
嗨 Mark,感谢你给我推荐了这个示例,非常完美(表格呈现略微不佳,但除此之外都非常准确)。 - HeavenCore

0
关于使用HTML Agility Pack进行此目的的问题,源代码下载中有一个名为Html2Txt的项目,位于htmlagilitypack-xxxx\Release\1_4_0\Html2Txt文件夹下。
HtmlConvert.cs包含了将HTML转换为文本并处理HTML注释等所有必要函数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接