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

2024-02-29

我正在使用这个脚本:

http://www.codeproject.com/Articles/11902/Convert-HTML-to-Plain-Text 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

Cheers

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

Edit 2:我注意到这个问题得到了很多人的关注,任何读到这篇文章的人都应该三思而行,考虑采用 regExp 方法,相反,我建议使用 Lynx(基于开源文本的浏览器)将 HTML 转换为纯文本,我问了一个类似的问题here https://stackoverflow.com/questions/13839225/handle-large-amounts-of-output-from-p-waitforexit-with-redirectstandardoutput我根据答案在编辑中提供了示例代码,这些代码应该帮助您开始在 .net 应用程序中使用 lynx.exe。这是我们最终使用的方法,此后没有出现任何问题。


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

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

尝试这个:

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

我强烈建议您不要使用正则表达式来解析 HTML。如果你使用HTML 敏捷包 http://html-agility-pack.net/?z=codeplex.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐