【问题标题】:Regex to get href value of particular <a> node [duplicate]正则表达式获取特定 <a> 节点的 href 值 [重复]
【发布时间】:2017-01-26 18:29:16
【问题描述】:

我有以下html内容

<html><head>
<title>Simple</title>


</head>
<body>
<div id="Content" style="padding: 5px;">
<p><a href="http://confluence:8080/download/attachments/8618175/Text.txt?version=1&modificationDate=1484637732181">Text.txt</a><br/>
<span class="image-wrap" style=""><img src="http://confluence:8080/download/attachments/8618175/add-button-blue-hi.png?version=1&modificationDate=1484562338796" style="border: 1px solid black" /></span><br/>
<span class="image-wrap" style=""><a class="confluence-thumbnail-link 300x200" href='http://confluence:8080/download/attachments/8618175/attachment.jpg'><img src="http://confluence:8080/download/thumbnails/8618175/attachment.jpg" style="border: 1px solid black" /></a></span></p>
</div>
</body></html>

这里我有两个&lt;a&gt; 标签。我需要 &lt;a&gt; 节点的 href 值,其中属性 'src' 是第二个 &lt;a&gt; 节点的值,即 \"http://confluence:8080/download/thumbnails/8618175/attachment.jpg\" 我有一个变量说字符串 x 包含这个值,我需要根据该值获取&lt;a&gt;节点的href。

现在我正在使用 "href\s*=\s*(?:\"(?[^\"]*)\"|(?\S+))" 但它给出了我是所有节点的 href 值。

【问题讨论】:

标签: c# regex


【解决方案1】:

我完全同意 Wiktor S 的观点。 IE。 HTML Agility Pack 是比 Regex 更强大的解决方案。但如果您必须使用正则表达式,试试这个...

<a[^>]*href\s*=(?<HRef>[^>]+)>

在这里测试:https://regex101.com/r/XuGjc5/1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 2016-10-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多