我需要正则表达式方面的帮助,以便从 C# 中的字符串中提取链接
我需要使用 C# 中的正则表达式从字符串中提取链接。我无法使用子字符串方法,因为字符串和链接中的字母可能会有所不同。 这是带有周围字母的链接:
-sv"><a href="http://sv.wikipedia.org/wiki/%C3%84pple" title="
-sv">
@"]*?HREF\s*=\s*[""']?([^'"" >]+?)[ '""]?[^>]*?>";
现在我在 stackoverflow 上向你们求助。 提前致谢! 最大限度
I need to extract a link from a string using regular expression in C#. I cannot use a substring method since both the letters in the string and the link may vary.
This is the link with surrounding letters:
-sv"><a href="http://sv.wikipedia.org/wiki/%C3%84pple" title="
The -sv"><a href="
part must be included in the regex or it won't be specific enough.
The end of the regex may be at the quotation markat the end of the link or whichever is the easiest way.
I've had another suggestion aswell, however, this does not include the sv-part in the beginning and the submitter couldnt make it compile:
@"]*?HREF\s*=\s*[""']?([^'"" >]+?)[ '""]?[^>]*?>";
Now I'm turning to you guys on stackoverflow.
Thanks in advance!
Max
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查问题:
用于解析超链接和描述的正则表达式
Check question:
Regex to Parse Hyperlinks and Descriptions
使用正则表达式从 html 中解析内容充满了危险。请参阅这个经典答案有力而幽默地解释了这一点。
你的问题的问题在于我们不知道背景。
标题
放在href
之前吗?有很多方法会出错...
但是,为了回答您的问题,此正则表达式模式将适用于您粘贴的确切字符串:
但是,您将无法执行直接用那个替换。请注意
()
,这是一个正则表达式捕获。我建议你自己查一下,这样你就不会永远是新手:)Parsing stuff out of html with regex is fraught with danger. Please see this classic answer which explains this with force and humour.
The problem with your question is that we don't know the context.
title
before thehref
?There are lots of ways to get it wrong...
However, to answer your question, this regex pattern will work for the exact string you have pasted:
However, you won't be able to do a replace directly with that. Note the
()
, this is a regex capture. I'd recommend looking that up yourself, that way you won't be a newbie forever :)尝试使用 HTML 解析器。源代码对于学习也非常直观。
下载库,添加对 HtmlAgilityPack.dll 的引用。获取所有链接:
Try using HTML parser. Source code is very intuitive for learning as well.
Download library, add reference to HtmlAgilityPack.dll. Get all your links with: