【问题标题】:RegEx Remove Internal Intranet Links from String .NETRegEx 从字符串 .NET 中删除内部 Intranet 链接
【发布时间】:2011-02-07 17:15:57
【问题描述】:

我正在寻找一种方法来删除字符串中对内部 Intranet 站点的所有引用,同时保留标签。

例如:

Dim str As String = Nothing
str &= "<a href=""http://intranet/somepage.asp"">Internal Page</a>"
str &= "<a href=""http://www.external.com"">External Page</a>"

任何引用 http://intranet 的内容都将被视为内部内容,需要使用正则表达式进行解析和删除。

感谢您的帮助。

谢谢

【问题讨论】:

    标签: .net regex parsing


    【解决方案1】:

    虽然它不是正则表达式解决方案,但它同样简单。鉴于上面的两个示例,您可以执行以下操作:

    Private Function IntranetCheck(ByVal link As String) As String
        If link.ToLower().Contains("http://intranet/") Then
            Return link.Split(">")(1).Split("<")(0)
        Else
            Return link
        End If
    End Function
    

    用法:

    Dim str As String = Nothing
    str &= IntranetCheck("<a href=""http://intranet/somepage.asp"">Internal Page</a>") 
    str &= IntranetCheck("<a href=""http://www.external.com"">External Page</a>")
    

    这将检查传入的字符串是否包含内网地址,如果包含则将字符串拆分为仅返回元素的内部文本。

    【讨论】:

      【解决方案2】:

      我强烈建议使用Html Agility Pack 进行此类处理。使用此工具,您可以执行以下操作:

      HtmlDocument doc;
      doc.Load(fileName);
      foreach(HtmlNode anchor in doc.DocumentNode.Descendants("a").Where(n => n.GetAttributeValue("href", string.Empty).Contains("intranet")))
      {
          // Change your href attribute here
          string newHref = anchor.GetAttributeValue("href", string.Empty).Replace("intranet", "somethingelse");
          anchor.SetAttributeValue("href", newHref);
      }
      

      【讨论】:

      • Jacob 我已经看到一些关于 Html Agility Pack 的提及,我可能需要开始研究它。感谢您的建议。
      • 当然可以。使用 html 时,这是绝对必要的 (IMO)。
      猜你喜欢
      • 2018-04-15
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多