【问题标题】:How to remove the embedded formatting如何删除嵌入的格式
【发布时间】:2016-04-17 16:27:56
【问题描述】:

由于我无法更改程序,因此我在此问题上没有任何其他选择,因此我需要一种以编程方式删除文本行中存在的百分号垃圾格式的方法:

查询将返回如下字符串:

'%3CSPAN style='FONT-SIZE: 12pt; FONT-FAMILY: %22Times New Roman%22,%22serif%22; mso-fareast-font-family: %22Times New Roman%22; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA'%3E%3CFONT color=#000000%3E3/20/18: Mrs. McDoogal completed a medical assessment with Dr. John Zoidberg, MD, at Futurama on 4/6/15 and he completed a new substance assessment on 4/14/18.%3C/FONT%3E%3CSPAN style=%22mso-spacerun: yes%22%3E%3CFONT color=#000000%3E  %3C/FONT%3E%3C/SPAN%3E%3CFONT color=#000000%3EMrs. McDoogal is diagnosed with Foobar I diagnosis of Groovy Mind, Foo; Cartoon Dependence; and Fiddling Disorder. %3C/FONT%3E%3CSPAN style=%22mso-spacerun: yes%22%3E%3CFONT color=#000000%3E %3C/FONT%3E%3C/SPAN%3E%3CFONT color=#000000%3EMr. McDoogal is prescribed DDT 30 mg. and LSD 150 mg ABC.%3C/FONT%3E%3CSPAN style=%22mso-spacerun: yes%22%3E%3CFONT color=#000000%3E  %3C/FONT%3E%3C/SPAN%3E%3CFONT color=#000000%3EMr. McDoogal will be enrolled in the day treatment program at Futurama.%3C/FONT%3E%3CSPAN style=%22mso-spacerun: yes%22%3E%3CFONT color=#000000%3E  %3C/FONT%3E%3C/SPAN%3E%3C/SPAN%3E'

我想去掉这样的东西:

.%3C/FONT%3E%3CSPAN style=%22mso-spacerun: yes%22%3E%3CFONT color=#000000%3E  %3C/FONT%3E%3C/SPAN%3E%3C/SPAN%3E

我想去掉的东西叫什么名字?

【问题讨论】:

  • 这不是“垃圾”,它可能是 HTML 或 URL 编码的字符值。
  • 这看起来像一个 URL 编码的字符串。签出:stackoverflow.com/questions/3778165/…
  • mso 标签表示它是直接从 Microsoft Office 产品复制的,因此正确地对其进行 URL 解码是不够的,但您可能还需要使用 RegEx 正确执行清理功能跨度>
  • 戴维,链接失效了。

标签: c#


【解决方案1】:

如果您对示例数据进行手动搜索和替换,使用以下值最终会得到一个 HTML 片段。

字符
%3C        
%3E        >        
%22        "        

进行这些替换会产生以下代码,此处为便于阅读而进行了格式化,但如果原始代码不包含行终止字符,则它可能全部为一行。

<SPAN style='FONT-SIZE: 12pt; FONT-FAMILY: "Times New Roman","serif"; mso-fareast-font-family: "Times New Roman"; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA'>
    <FONT color=#000000>3/20/18: Mrs. McDoogal completed a medical assessment with Dr. John Zoidberg, MD, at Futurama on 4/6/15 and he completed a new substance assessment on 4/14/18.</FONT>
    <SPAN style="mso-spacerun: yes">
        <FONT color=#000000>  </FONT>
    </SPAN>
    <FONT color=#000000>Mrs. McDoogal is diagnosed with Foobar I diagnosis of Groovy Mind, Foo; Cartoon Dependence; and Fiddling Disorder. </FONT>
    <SPAN style="mso-spacerun: yes">
        <FONT color=#000000> </FONT>
    </SPAN>
    <FONT color=#000000>Mr. McDoogal is prescribed DDT 30 mg. and LSD 150 mg ABC.</FONT>
    <SPAN style="mso-spacerun: yes">
        <FONT color=#000000>  </FONT>
    </SPAN>
    <FONT color=#000000>Mr. McDoogal will be enrolled in the day treatment program at Futurama.</FONT>
    <SPAN style="mso-spacerun: yes">
        <FONT color=#000000>  </FONT>
    </SPAN>
</SPAN>

您可以在 c# 中使用String.Replace 方法执行此操作:

public static ReplaceGarbage(String garbageString)
{
    return garbageString.Replace(@"%3C", @"<")
                        .Replace(@"3E", @">")
                        .Replace(@"%22", @"""");
}

那么删除标签(如果你需要的话)应该是一项相对容易的工作,只留下正文。

public static string StripTagsRegex(string source)
{
    return Regex.Replace(source, "<.*?>", string.Empty);
}

【讨论】:

  • 谢谢,这正是我正在寻找的解决方案。
  • @Mojoala 欢迎来到c#。我的答案中的代码远非完美,但我很高兴它有所帮助,并希望为您提供了扩展的起点。
猜你喜欢
  • 2021-11-27
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 2021-11-07
相关资源
最近更新 更多