【问题标题】:removing MS Office junk from a text string从文本字符串中删除 MS Office 垃圾
【发布时间】:2023-03-23 12:05:01
【问题描述】:

我有一个包含一堆 MS Word 垃圾的字符串,如下所示:

<!--[if gte mso 9]><xml>
<o:OfficeDocumentSettings>

</xml><![endif]--><!--[if gte mso 9]><xml>
 <w:WordDocument>
  <w:View>Normal</w:View>
  <m:mathPr>
   <m:mathFont m:val="Cambria Math"/>
   <m:brkBin m:val="before"/>
  </m:mathPr></w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]>

<style>
 /* Style Definitions */
 table.MsoNormalTable
    {mso-style-name:"Table Normal";
    mso-tstyle-rowband-size:0;
    mso-hansi-font-family:Calibri;
    mso-hansi-theme-font:minor-latin;}
</style>
<![endif]-->

我已尝试使用以下功能将其删除,但它们仅删除部分并留下大量空白:

Public Function CleanOfficeJunk(html As String) As String
    ' start by completely removing all unwanted tags 
    html = System.Text.RegularExpressions.Regex.Replace(html, "<[/]?(font|span|xml|del|ins|[ovwxp]:\w+)[^>]*?>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    ' then run another pass over the html (twice), removing unwanted attributes 
    html = System.Text.RegularExpressions.Regex.Replace(html, "<([^>]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>", "<$1$2>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    html = System.Text.RegularExpressions.Regex.Replace(html, "<([^>]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>", "<$1$2>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    Return html
End Function

我在 SQL Server 报告服务 (SSRS) 报告中使用它,需要先清理字符串,然后再将它们显示在文本框中。

有没有更好的方法来删除这样的东西?

编辑:我确实看到了这篇文章 Remove HTML comments with Regex, in Javascript

但接受的答案似乎不适用于我的情况。

【问题讨论】:

  • 您的字符串是否可能包含良好的&lt; OR &gt;。好像不是,为什么不删除 &lt; and &gt; 中包含的所有内容...
  • 它可以在这里或那里包含一个
    。谢谢
  • 最后一个问题,你只想要你想要指定的标签之间的文字吗?基本上只针对您允许的标签
  • @Codexer 我只想删除 xml 和样式标签之间的所有内容。如果有类似的东西,你好...那么我想保留它,谢谢

标签: .net regex vb.net ssrs-2008 ms-office


【解决方案1】:

您应该尝试将 PlaceHolder 属性设置为 HTML。这解决了我的问题。

【讨论】:

    最近更新 更多