【问题标题】:Regular Expression - Remove HTML comment spanning multiple line breaks正则表达式 - 删除跨越多个换行符的 HTML 注释
【发布时间】:2012-05-18 16:10:57
【问题描述】:

我正在使用这个脚本:

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(基于开源文本的浏览器)进行转换HTML 到纯文本,我问了一个类似的问题here,我根据应该让您开始在 .net 应用程序中使用 lynx.exe 的答案在编辑中提供了示例代码。这是我们最终使用的方法,从那以后没有任何问题。

【问题讨论】:

标签: c# regex vb.net replace


【解决方案1】:

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

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

试试这个:

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

我强烈建议您不要使用正则表达式来解析 HTML。如果你改用HTML Agility Pack,你会为自己省去一个痛苦的世界。

【讨论】:

  • 对不起,马克,请参阅编辑 1 - 两者都没有删除评论部分
  • 嗨,马克,&lt;!--.*?--&gt; + Singleline 也不起作用,但是我接受了建议并放弃了 reg ex 选项,我现在正在研究 HTML Agilty Pack 解决方案并将更新如果我遇到任何问题/找到解决方案,我的问题。
  • 嗨,马克,为我指点样品干杯,那是完美的(桌子使它有点差,但除了那个点)。
【解决方案2】:

关于为此目的使用 HTML Agility Pack,有一个名为 Html2Txt 的项目 在 htmlagilitypack-xxxx\Release\1_4_0\Html2Txt 文件夹下的源下载中。

HtmlConvert.cs 包含将 HTML 转换为文本和处理 HTML cmets 等所有必要的函数。

【讨论】:

    猜你喜欢
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 2017-07-26
    • 2011-02-04
    • 1970-01-01
    • 2010-11-08
    相关资源
    最近更新 更多