【问题标题】:Regex match pull href link and string in between <a></a> [duplicate]正则表达式匹配 <a></a> 之间的 pull href 链接和字符串 [重复]
【发布时间】:2011-08-20 08:47:31
【问题描述】:

可能重复:
RegEx to return 'href' attribute of 'link' tags only?

这是我的字符串,我想使用 C# Regex 从 href="pull this out" 和标签之间的文本中提取链接。不知道该怎么做。

<a href="http://msdn.microsoft.com/en-us/library/Aa538627.aspx" onclick="trackClick(this, '117', 'http\x3a\x2f\x2fmsdn.microsoft.com\x2fen-us\x2flibrary\x2fAa538627.aspx', '15');">ToolStripItemOwnerCollectionUIAdapter.GetInsertingIndex Method ...</a>

【问题讨论】:

  • 不要使用正则表达式解析 HTML。
  • 已经有几十个正则表达式问题,你的答案是一个简单的搜索。您应该能够轻松地根据您的问题调整此答案:stackoverflow.com/questions/268338/…
  • 查找 HTML 敏捷包。并尝试搜索“HTML Parsing C#”,你会得到很多关于这个确切答案的问题。

标签: c# regex


【解决方案1】:

不要使用正则表达式来解析 HTML(正如@hsz 提到的)。看看为什么:RegEx match open tags except XHTML self-contained tags。取而代之的是,您可以使用像 HtmlAgilityPack 这样的 HTML 解析器:

var html = @"<a href=""http://msdn.microsoft.com/en-us/library/Aa538627.aspx"" onclick=""trackClick(this, '117', 'http\x3a\x2f\x2fmsdn.microsoft.com\x2fen-us\x2flibrary\x2fAa538627.aspx', '15');"">ToolStripItemOwnerCollectionUIAdapter.GetInsertingIndex Method ...</a>";
HtmlDocument document = new HtmlDocument();
document.LoadHtml(html);

var link = document.DocumentNode.SelectSingleNode("//a");
if (link != null)
{
    var href = link.Attributes["href"].Value;
    var innerText = link.InnerText;
}

现在href 包含http://msdn.microsoft.com/en-us/library/Aa538627.aspxinnerText(又名标签之间的字符串)包含ToolStripItemOwnerCollectionUIAdapter.GetInsertingIndex Method ...

是不是比正则表达式更容易?

【讨论】:

    【解决方案2】:

    这显示了如何做你正在寻找的东西:C# Scraping HTML Links

    这是该页面的代码示例:

    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    public struct LinkItem
    {
        public string Href;
        public string Text;
    
        public override string ToString()
        {
        return Href + "\n\t" + Text;
        }
    }
    
    static class LinkFinder
    {
        public static List<LinkItem> Find(string file)
        {
        List<LinkItem> list = new List<LinkItem>();
    
        // 1.
        // Find all matches in file.
        MatchCollection m1 = Regex.Matches(file, @"(<a.*?>.*?</a>)",
            RegexOptions.Singleline);
    
        // 2.
        // Loop over each match.
        foreach (Match m in m1)
        {
            string value = m.Groups[1].Value;
            LinkItem i = new LinkItem();
    
            // 3.
            // Get href attribute.
            Match m2 = Regex.Match(value, @"href=\""(.*?)\""",
            RegexOptions.Singleline);
            if (m2.Success)
            {
            i.Href = m2.Groups[1].Value;
            }
    
            // 4.
            // Remove inner tags from text.
            string t = Regex.Replace(value, @"\s*<.*?>\s*", "",
            RegexOptions.Singleline);
            i.Text = t;
    
            list.Add(i);
        }
        return list;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-30
      • 2011-06-10
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 2014-08-30
      相关资源
      最近更新 更多