Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match URL in c#?

Tags:

c#

regex

url

I have found many examples of how to match particular types of URL-s in PHP and other languages. I need to match any URL from my C# application. How to do this? When I talk about URL I talk about links to any sites or to files on sites and subdirectiories and so on.

I have a text like this: "Go to my awsome website http:\www.google.pl\something\blah\?lang=5" or else and I need to get this link from this message. Links can start only with www. too.

like image 638
Tom Smykowski Avatar asked Aug 24 '09 16:08

Tom Smykowski


1 Answers

Microsoft has a nice page of some regular expressions...this is what they say (works pretty good too)

^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$

http://msdn.microsoft.com/en-us/library/ff650303.aspx#paght000001_commonregularexpressions

like image 92
Chuck Rostance Avatar answered Oct 02 '22 14:10

Chuck Rostance