【问题标题】:RegEx - Remove HTML hyperlinks based on the link textRegEx - 根据链接文本删除 HTML 超链接
【发布时间】:2010-04-23 15:31:13
【问题描述】:

我有一些包含 HTML 超链接的文本。 我想删除超链接,但只删除特定的。

例如我从这个开始:

This is text <a href="link/to/somewhere">Link to Remove</a> and more text with another link <a href="/link/to/somewhere/else">Keep this link</a>

我想拥有:

This is text and more text with another link <a href="/link/to/somewhere/else">Keep this link</a> 

我有这个正则表达式,

<a\s[^>]*>.*?</a>

...但它匹配所有链接。

我需要在该表达式中添加什么内容才能仅匹配带有链接文本“删除”(例如)的链接?

提前致谢。

【问题讨论】:

标签: c# html regex


【解决方案1】:

您可能会收到很多关于不要在 HTML 上使用正则表达式的反馈...但是如果您决定使用正则表达式,请尝试以下操作:

 <a\s[^>]*>.*?Remove.*?</a>

这是链接文本中“删除”的位置。

【讨论】:

  • 谢谢,明白了。如果我想在不区分大小写的情况下匹配“删除”,我会用什么包装? (例如,匹配 'Remove' 或 'remove' 或 'REMOVE' 等...)
  • @Rob:很确定 C# 有类似 RegexOptions.IgnoreCase 的东西,您可以将其作为另一个参数传入。
【解决方案2】:
$str=~/(.*)<a.*<\/a>([a-z ]+ <a.*<\/a>)/;
print "$1$2";

【讨论】:

    【解决方案3】:

    (.*?)(.*)

    重建:$1$2

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 2011-07-18
      • 2023-04-07
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      • 2021-06-20
      • 2016-06-01
      相关资源
      最近更新 更多