【问题标题】:Regular Expression to find out <a> tag and get the value of HREF using C# [duplicate]使用 C# 查找 <a> 标记并获取 HREF 值的正则表达式 [重复]
【发布时间】:2015-08-17 16:14:49
【问题描述】:

有人可以帮我解决以下问题

我有一个存储 html 的字符串,需要找到锚标记并使用 C# 获取该标记的 href 值

例如:

<p>can someone please help me for below </p>
<p>for Link <a href="www.ffb.cc">Click Here</a></p>

我需要得到 href 值 ..generic 代码赞赏

编辑:

试过&lt;a\s+(?:[^&gt;]*?\s+)?href="([^"]*)"

但没有找到匹配项

注意:由于信誉点,我无法发布图片:(

【问题讨论】:

标签: c# .net


【解决方案1】:

不要使用Regex to parse html。正确的方法是使用像 HmlAgilityPack

这样的 html 解析器
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlstring);

var links = doc.DocumentNode.SelectNodes("//a")
               .Select(a => a.Attributes["href"].Value)
               .ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    相关资源
    最近更新 更多