Regex - Validate Url

S

shapper

Hello,

I need to validate an url in my C# code.
Does anyone knows a reliable Regex to validate a Url?

Thanks,
Miguel
 
S

shapper

And be careful, since the most of the examples floating around on the
web only show how to validate a subset of valid URLs, which may or may
not be acceptable depending on your needs.

I am using the following:
Boolean valid = Regex.IsMatch(context.PropertyValue, @"((?<=\()
[A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\
+,;=]|%[A-Fa-f0-9]{2})+(?=\)))|([A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-
z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+)");

Based on the following Regex:
http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris

Not sure if it is the best one but it seems ok.

Basically in my case I am adding Slides (Images) to site in a CMS.
Each image can have a link to some site.
I want to be sure that the url inserted has a valid format.

Thanks,
Miguel
 
J

Jesse Houwing

* shapper wrote, On 10-9-2009 17:15:
And be careful, since the most of the examples floating around on the
web only show how to validate a subset of valid URLs, which may or may
not be acceptable depending on your needs.

I am using the following:
Boolean valid = Regex.IsMatch(context.PropertyValue, @"((?<=\()
[A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\
+,;=]|%[A-Fa-f0-9]{2})+(?=\)))|([A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-
z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+)");

Based on the following Regex:
http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris

Not sure if it is the best one but it seems ok.

Basically in my case I am adding Slides (Images) to site in a CMS.
Each image can have a link to some site.
I want to be sure that the url inserted has a valid format.

Thanks,
Miguel

If you want to make sure you can open the image and that the url
actually exists, the best way is to use the URI class and actually try
to open the resource using WebClient.

The definition of a URL/URI is so broad and can contain so many
protocols and subsequent URI scheme's that a regex probably only works
on the most standard cases.
 
S

shapper

* shapper wrote, On 10-9-2009 17:15:


I am using the following:
       Boolean valid = Regex.IsMatch(context.PropertyValue, @"((?<=\()
[A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\
+,;=]|%[A-Fa-f0-9]{2})+(?=\)))|([A-Za-z][A-Za-z0-9\+\.\-]*:([A-Za-
z0-9\.\-_~:/\?#\[\]@!\$&'\(\)\*\+,;=]|%[A-Fa-f0-9]{2})+)");
Not sure if it is the best one but it seems ok.
Basically in my case I am adding Slides (Images) to site in a CMS.
Each image can have a link to some site.
I want to be sure that the url inserted has a valid format.
Thanks,
Miguel

If you want to make sure you can open the image and that the url
actually exists, the best way is to use the URI class and actually try
to open the resource using WebClient.

The definition of a URL/URI is so broad and can contain so many
protocols and subsequent URI scheme's that a regex probably only works
on the most standard cases.

No no ... The url I am mentioned is something like http://www.google.com
or http://www.thiswebsite.com/somesection, etc.
So when the user clicks that image it is redirected to the URL.
I am just testing if the user inserts a valid URL ... of course the
URL might not lead to nowhere by mistake but at least is a correct
url.
Just like a banner ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Uri 3
Regex.Validate Date and Time 5
Difficulty with Regex pattern to validate a string 11
RegEx for Hex string validation 5
Null and Generic Type 2
Validate a string format 5
The Regex problem 4
Regex password 2

Top