Share via


URL validation using regex

Question

Thursday, January 11, 2018 6:36 AM

I am using to validate URL with or without http/https using the following code snippet 

Regex urlRegex = new Regex(@"(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?");
return urlRegex.IsMatch(url);

But when i input links like ww.google.com still it validates as correct. So what is the correct regex should i used to overcome this problem. Anyway the URL should be validated with or without http/https.Any help would be much appreciated.

All replies (5)

Thursday, January 11, 2018 6:41 AM

Try 

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

Mark Answered, if it solves your question and Vote if you found it helpful.
Rohit Arora


Thursday, January 11, 2018 10:39 AM

No, still it doesn't work for www.google.com and ww.google.com


Thursday, January 11, 2018 2:44 PM | 1 vote

Please don't try to do this. The rules for validating a URL cannot be properly handled in an RE. At best it'll match invalid URLs. At worst it will fail valid ones. While you may be able to come up with the basic pattern you cannot enforce all the URL rules in RE. 

.NET already has support for validating URLs via the Uri class. Please explain why you aren't using this type instead (which is already tested and works properly in 1000s of project in use today).

Michael Taylor http://www.michaeltaylorp3.net


Sunday, January 14, 2018 12:29 PM

Hello Rishantha,

Try to use Uri.IsWellFormedUriString to check whether the URL is in correct form rather than regex.

https://msdn.microsoft.com/en-us/library/system.uri.iswellformeduristring%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

According to your example, it seem that you are verifying if a url exists, because a url could be started with any number of 'w' characters. A generic URI is of the form:

** scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]**

To check the existence of a URL create a WebRequest:

WebRequest request = WebRequest.Create("http://www.google.com");
try
{
     request.GetResponse();
}
catch //If exception thrown then couldn't get response from address
{
     /// The URL is incorrect
     MessageBox.Show("The URL is incorrect");
}

Best regards,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Sunday, January 28, 2018 1:04 PM

Hello Rishantha,

Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

Thank you for your understanding and cooperation.

Best regards,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].