Hea*_*ore 5 c# regex vb.net replace
我正在使用这个脚本:
http://www.codeproject.com/Articles/11902/Convert-HTML-to-Plain-Text
将一些outlook HTML转换为纯文本.
它几乎可以工作,它留下的唯一的东西是<!-- -->除了<style>标签(被删除)后面的html注释标签中的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> </o:p></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Arial","sans-serif";color:dimgray;mso-fareast-language:EN-GB">JOE BLOGS</span></b><span style="font-size:10.0pt;font-family:"Arial","sans-serif";color:dimgray;mso-fareast-language:EN-GB">
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是生成的文本:(注意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
JOE BLOGS
Run Code Online (Sandbox Code Playgroud)
我尝试过使用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)
Run Code Online (Sandbox Code Playgroud)
请帮助 - 这是一个2分钟的工作,我从午餐时间以来一直坚持下去
干杯
编辑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)
Run Code Online (Sandbox Code Playgroud)
编辑2:我注意到这个问题得到了很多观点,任何阅读此内容的人都应该三思而后行采用regExp方法,而我建议使用Lynx(OpenSource基于文本的浏览器)将HTML转换为纯文本,我问了一个类似的问题在这里,我根据应该从.net应用程序中开始使用lynx.exe的答案提供编辑中的示例代码.这是我们最终使用的方法,从那时起就没有任何问题.
你的第二个正则表达式有三个原因:
.匹配任何角色.*是贪婪.你想*?懒洋洋地匹配.RegexOptions.Singleline.试试这个:
result = Regex.Replace(result, "<!--.*?-->", "", RegexOptions.Singleline);
Run Code Online (Sandbox Code Playgroud)
我强烈建议您不要使用正则表达式来解析HTML.如果您使用HTML Agility Pack,您将为自己节省一个痛苦的世界.