2017-04-18 21 views
0

背景正則表達式表達不

目前有一個控制檯應用程序,從0365 Outlook帳戶獲取電子郵件,我使用Outlook API 2.0

從HTML字符串刪除內聯CSS問題

我正在使用api訪問電子郵件的正文,但正文是以html字符串形式出現的。我正在使用我的去正則表達式功能,它將刪除html標記,但outlook添加一個CSS類到他們的Html基本上使我的正則表達式過時。

代碼

string body = "<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<meta content="text/html; charset=us-ascii"> 
<meta name="Generator" content="Microsoft Word 15 (filtered medium)"> 
<style> 
<!-- 
@font-face 
    {font-family:"Cambria Math"} 
@font-face 
    {font-family:Calibri} 
p.MsoNormal, li.MsoNormal, div.MsoNormal 
    {margin:0in; 
    margin-bottom:.0001pt; 
    font-size:11.0pt; 
    font-family:"Calibri",sans-serif} 
a:link, span.MsoHyperlink 
    {color:#0563C1; 
    text-decoration:underline} 
a:visited, span.MsoHyperlinkFollowed 
    {color:#954F72; 
    text-decoration:underline} 
span.EmailStyle17 
    {font-family:"Calibri",sans-serif; 
    color:windowtext} 
.MsoChpDefault 
    {font-family:"Calibri",sans-serif} 
@page WordSection1 
    {margin:1.0in 1.0in 1.0in 1.0in} 
div.WordSection1 
    {} 
--> 
</style> 
</head> 
<body lang="EN-US" link="#0563C1" vlink="#954F72"> 
<div class="WordSection1"> 
<p class="MsoNormal">&nbsp;</p> 
</div> 
<hr> 
<p><b>Confidentiality Notice:</b> This e-mail is intended only for the addressee named above. It contains information that is privileged, confidential or otherwise protected from use and disclosure. If you are not the intended recipient, you are hereby notified 
that any review, disclosure, copying, or dissemination of this transmission, or taking of any action in reliance on its contents, or other use is strictly prohibited. If you have received this transmission in error, please reply to the sender listed above 
immediately and permanently delete this message from your inbox. Thank you for your cooperation.</p> 
</body> 
</html> 
"; 
string viewString1 = Regex.Replace(body, "<.*?>", string.Empty); 
string viewString12 = viewString1.Replace("&nbsp;", string.Empty); 
從我的正則表達式

<!-- 
@font-face 
    {font-family:"Cambria Math"} 
@font-face 
    {font-family:Calibri} 
p.MsoNormal, li.MsoNormal, div.MsoNormal 
    {margin:0in; 
    margin-bottom:.0001pt; 
    font-size:11.0pt; 
    font-family:"Calibri",sans-serif} 
a:link, span.MsoHyperlink 
    {color:#0563C1; 
    text-decoration:underline} 
a:visited, span.MsoHyperlinkFollowed 
    {color:#954F72; 
    text-decoration:underline} 
span.EmailStyle17 
    {font-family:"Calibri",sans-serif; 
    color:windowtext} 
.MsoChpDefault 
    {font-family:"Calibri",sans-serif} 
@page WordSection1 
    {margin:1.0in 1.0in 1.0in 1.0in} 
div.WordSection1 
    {} 
--> 







Confidentiality Notice: This e-mail is intended only for the addressee named above. It contains information that is privileged, confidential or otherwise protected from use and disclosure. If you are not the intended recipient, you are hereby notified 
that any review, disclosure, copying, or dissemination of this transmission, or taking of any action in reliance on its contents, or other use is strictly prohibited. If you have received this transmission in error, please reply to the sender listed above 
immediately and permanently delete this message from your inbox. Thank you for your cooperation. 

目的

結果

我需要從字符串能夠帶html標籤,並且還刪除出來的css類尋找身體的地方。

+1

順便說一句,你可能要考慮更換 爲空(白)的空間,這是它代表的(不是空的)。 – JuanR

回答

3

您可以String.Emptyregex optionSingleline替換<!--.*?-->(使.匹配新行)

string viewString1 = Regex.Replace(body, "<.*?>", string.Empty, RegexOptions.Singleline);