开发者

c# fetch anything that looks like a URL

开发者 https://www.devze.com 2022-12-26 23:47 出处:网络
i am trying to fetch anything that looks like a URL from a string how would i do this with c#? most of the are going to look like this: http:/开发者_如何学运维/www.something.com, but some may look l

i am trying to fetch anything that looks like a URL from a string

how would i do this with c#?

most of the are going to look like this: http:/开发者_如何学运维/www.something.com, but some may look like this: http://something.somethingelse.com


Assuming you're parsing some [X]HTML - Use the HTML Agility Pack.

Straight from the examples page:

 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
 {
    HtmlAttribute att = link["href"];

    //Do stuff
 }


You'll want to use RegEx for that. Here's a handy article that will get you started. It's an ASP.NET article that wants to find URLs and turn them into hyperlinks but performs the activity you're interested in.


This really depends on the context, but you will generally use regular expressions (System.Text.RegularExpressions.Regex class). To get more specific answers, like code, you will need to provide what the surrounding string might look like so that we know how to differentiate url from non-url.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号