c# - 用于删除文字格式的正则表达式模式

标签 c# html regex

我在使用正则表达式模式替换 HTML 标记中的项目时遇到问题。我要替换的值是:

<!--[if mso 9]-->
<style>
 p.MsoNormal<br />
 {margin-left:18.75pt;}<br />
</style>
<!--[endif]-->

class="MsoNormal"

我不是最擅长使用正则表达式的,到目前为止我想出的模式是:

  1. <!--(.*?)-->
  2. class=\"msonormal\"
  3. class=\"MsoNormal\"

第 1 项删除所有以 <!-- 开头的内容并以 --> 结尾但它只会在不删​​除所有实例时执行。

我认为第 2 项和第 3 项根本不起作用。

我在这里找到了一些关于模式的信息:

is there a Way to strip all Unnecessary MS Word Formatting from FCKEditor

我要删除的完整文本列表是这样的:

    <!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]-->
<title>Blank</title>
<style>
    <!--
/* Font Definitions */
@font-face
    {font-family:Helvetica;
    panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
    {font-family:"Cambria Math";
    panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
    {font-family:Calibri;
    panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
    {font-family:Tahoma;
    panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
    {font-family:"Arial Black";
    panose-1:2 11 10 4 2 1 2 2 2 4;}
@font-face
    {font-family:"Palatino Linotype";
    panose-1:2 4 5 2 5 5 5 3 3 4;}
@font-face
    {font-family:"Trebuchet MS";
    panose-1:2 11 6 3 2 2 2 2 2 4;}
@font-face
    {font-family:"Matura MT Script Capitals";
    panose-1:3 2 8 2 6 6 2 7 2 2;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin-top:0in;
    margin-right:0in;
    mso-margin-bottom-alt:auto;
    margin-left:0in;
    font-size:10.0pt;
    font-family:"Helvetica","sans-serif";
    color:#FFFFCC;
    mso-believe-normal-left:yes;}
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;}
p
    {mso-style-priority:99;
    mso-margin-top-alt:auto;
    margin-right:0in;
    mso-margin-bottom-alt:auto;
    margin-left:0in;
    font-size:12.0pt;
    font-family:"Times New Roman","serif";}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
    {mso-style-priority:99;
    mso-style-link:"Balloon Text Char";
    margin-top:0in;
    margin-right:0in;
    mso-margin-bottom-alt:auto;
    margin-left:0in;
    font-size:8.0pt;
    font-family:"Tahoma","sans-serif";
    color:#FFFFCC;}
p.MsoNoSpacing, li.MsoNoSpacing, div.MsoNoSpacing
    {mso-style-priority:1;
    margin:0in;
    margin-bottom:.0001pt;
    font-size:12.0pt;
    font-family:"Times New Roman","serif";}
span.BalloonTextChar
    {mso-style-name:"Balloon Text Char";
    mso-style-priority:99;
    mso-style-link:"Balloon Text";
    font-family:"Tahoma","sans-serif";
    color:#FFFFCC;}
span.EmailStyle21
    {mso-style-type:personal;
    font-family:"Arial","sans-serif";
    color:black;}
span.EmailStyle22
    {mso-style-type:personal;
    font-family:"Arial","sans-serif";
    color:#0F243E;}
span.EmailStyle23
    {mso-style-type:personal;
    font-family:"Calibri","sans-serif";
    color:#1F497D;}
span.EmailStyle24
    {mso-style-type:personal;
    font-family:"Arial","sans-serif";
    color:#0F243E;}
span.EmailStyle25
    {mso-style-type:personal;
    font-family:"Calibri","sans-serif";
    color:#1F497D;}
span.EmailStyle26
    {mso-style-type:personal;
    font-family:"Calibri","sans-serif";
    color:#1F497D;}
span.EmailStyle27
    {mso-style-type:personal;
    font-family:"Calibri","sans-serif";
    color:#1F497D;}
span.EmailStyle28
    {mso-style-type:personal;
    font-family:"Calibri","sans-serif";
    color:#1F497D;}
span.EmailStyle29
    {mso-style-type:personal;
    font-family:"Calibri","sans-serif";
    color:windowtext;}
span.EmailStyle30
    {mso-style-type:personal;
    font-family:"Calibri","sans-serif";
    color:#1F497D;}
span.EmailStyle31
    {mso-style-type:personal;
    font-family:"Calibri","sans-serif";
    color:#1F497D;}
span.EmailStyle33
    {mso-style-type:personal-reply;
    font-family:"Calibri","sans-serif";
    color:#1F497D;}
.MsoChpDefault
    {mso-style-type:export-only;
    font-size:10.0pt;}
@page WordSection1
    {size:8.5in 11.0in;
    margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
    {page:WordSection1;}
-->
</style>
<!--[if mso 9]-->
<style>
    p.MsoNormal
    {margin-left:18.75pt;}
</style>
<!--[endif]--><!--[if gte mso 9]>
<o:shapedefaults v:ext="edit" spidmax="1026" />
<![endif]--><!--[if gte mso 9]>


<p class="MsoNormal"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: #1f497d;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11pt; font-family: Calibri, sans-serif; color: #1f497d;"><o:p>&nbsp;</o:p></span></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>

最佳答案

尝试使用此模式来捕获第一组项目:

\<\!\-\-\[i[\w\s\p{P}\p{S}]+if\]\-\-\>

关键是要尽可能避免 .*,因为它会吃掉整个输入字符串,因此结束条件不匹配。

对于第二个和第三个,你只需要转义=符号,如\=。它们会很好,但是也可以尝试使用以下模式来捕获其他 Mso* 类:

class\=\"Mso[^\"]+\"

关于c# - 用于删除文字格式的正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13400421/

相关文章:

html - 每当我将段落放在页面下方时,我无法让我的图像和 'About me' 跨度留在页面的右侧

c# - 如何在控制台应用程序中使用 Entity Framework 从数据库填充 html 中的字段?

html - 添加填充到 fa-fw Font Awesome 图标

javascript - 无法在 javascript 正则表达式中匹配 '£'(英镑)

c# - XDocument.Parse 由于解析错误而失败,如何禁用解析

C# 正则表达式 - 匹配和替换,自动递增

C# Xamarin - Popover segue 导致匿名构建错误

c# - 如何将文件拖放到 DataGridView 并将文件名记录到单元格 C# 中?

javascript string regex替换引号之间的字符串

Java正则表达式匹配 "string"