How to match URL in c#?
Solution 1
If you need to test your regex to find URLs you can try this resource
It will test your regex while you're writing it.
In C# you can use regex for example as below:
Regex r = new Regex(@"(?<Protocol>\w+):\/\/(?<Domain>[\w@][\w.:@]+)\/?[\w\.?=%&=\-@/$,]*");
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
while (m.Success)
{
//do things with your matching text
m = m.NextMatch();
}
Solution 2
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
Solution 3
I am not sure exactly what you are asking, but a good start would be the Uri class, which will parse the url for you.
Solution 4
Here's one defined for URL's.
^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$
http://msdn.microsoft.com/en-us/library/ms998267.aspx
Solution 5
Regex regx = new Regex("http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
Related videos on Youtube
Tom Smykowski
Updated on September 04, 2021Comments
-
Tom Smykowski over 2 years
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.
-
Patrick Klug almost 13 years+1 : although you forgot to add the @ symbol in front of the string.
-
michele over 12 years@Rumplin: can you explain the error you get? if i try with gskinner, the regex matches correctly test.com or do you intend th whole string with the '' ?
-
Rumplin over 12 yearsyou have to look at the actual url, not the one shown. Stackoverflow corrects links... 'h t t p : / / h t t p : / /' without spaces
-
Jared Updike over 8 yearsThis could be more specific: for example: var myUri = null; Uri.TryCreate(str, UriKind.Absolute, out myVar); and check if myUri changed from null to non-null.
-
Wolf5 over 7 yearsThat one does not include the query string behind the url. it stops at the first '=' letter. To fix, just add the single character '=' so it ends like this "....&%\$#_=]*)?$". Also, that regex pattern will not find an url in string. It will only tell you that the string is an url or not. To find it, ommit "^" at the start and "$" at the end. Regex.Matches(text, pattern) should then return all urls in the text.
-
user about 7 yearsDoes it include mailto urls?
-
Tom Rutchik over 2 yearsthis case fails: example.com:81/path/index.php?query=toto+le+heros#top It can be corrected with (ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#=_]*)? It also doesn't work for url containing a username@hostname