【问题标题】:VB.Net or C# to strip html but leave less than or greater thanVB.Net 或 C# 剥离 html 但留下小于或大于
【发布时间】:2012-04-10 19:54:11
【问题描述】:

我有一个包含以下 html 数据的字符串变量:

<p> <em><strong>This is some <span style="background-color: rgb(255, 255, 0);">rich </span>text. 3 < 5 is a valid statement. <br /> </strong></em></p>

我需要能够删除 html,但要留下任何小于或大于符号,以防数据包含数学方程式(如字符串的“3

&lt;[^&gt;]*&gt;
&lt;[^&gt;]+&gt;
&lt;(.|\n)*?&gt;
\&lt;[^\&gt;]*\&gt;

我也试过this link上的代码,但它也不能处理小于/大于符号。

非常感谢任何建议。

【问题讨论】:

  • 第一个建议正则表达式的人能被枪毙吗?
  • 不确定我是否同意,在这种特殊情况下。用正则表达式解析 HTML 是一个坏主意 (TM),但在这种情况下,他想删除与正则表达式匹配的所有标签。在这种情况下解析 HTML 也会很烦人,因为您必须遍历整个树来连接您的字符串。除非解析库可以做到这一点?

标签: html regex vb.net


【解决方案1】:

用''替换所有匹配的文本

(<[^<>]*>)+

(我在 Rubular.com 上对其进行了测试,但它也应该适用于 C#。)

显然代码应该是

RegexObj.Replace('<p> <em><strong>This is some <span style="background-color: rgb(255, 255, 0);">rich </span>text. 3 < 5 is a valid statement. <br /> </strong></em></p>', "")

【讨论】:

  • 谢谢大卫!这解决了我的问题。我真的很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 2013-09-02
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多