【发布时间】:2020-10-14 15:59:45
【问题描述】:
我需要从两种 html 中抓取 https 链接
一个是这样的
<a href="javascript:void(0)" onclick="javascript:newwindow1('https://hello.com/uploads/order/8c25ce592gfgfgfh99.pdf');">
this is some content Lorem Ipsum Lorem Ipsum Lorem Ipsum <img src="/img/pdf.jpg" width="15"></a
另一个是这样的
<a href="javascript:void(0)" onclick="javascript:newwindow1('https://hello.com//webadmin/pdf/order/2018/Aug/hello this is regarding an older document Ors._2018-08-31 12:09:12.pdf');">
this is some content Lorem Ipsum Lorem Ipsum Lorem Ipsum <img src="/img/pdf.jpg" width="15"></a>
两者的区别在于newwindow1中的链接,因为第二个html链接包含很少的空格
并且链接包含stringpdf两次
现在我想从他们两个中提取链接
我正在使用c#
Regex.Match(HtmlString, @"('https[^\s]+.pdf')");
通过这种方式,我可以从第一个 html 中提取链接,但在第二个 html 中,它的提取方式是这样的
https://hello.com//webadmin/pdf/
从https 开始,在pdf 停止,但链接尚未完成
除了regex,请告诉我html agility pack是否可以这样做
【问题讨论】:
-
如果您的属性值总是用单引号分隔,您可以使用
[^']而不是[^\s]、('https[^']+\.pdf')。转义模式中的点以匹配文字点。 -
感谢@WiktorStribiżew,它肯定会起作用,您能否将其添加为答案
标签: javascript c# regex html-agility-pack