Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Need a RegEx (for ASP.NET) which detects a web URL

I need a RegEx that can pick up  that a string is a valid web URL.

There must be many. Has anyone for a decent one? And can you show me how to pop it into my C# code and perform the test?

Thanks.
SOLUTION
Avatar of yeelokhk
yeelokhk
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You might find this page (link to your answer as well) a good resource for the future:

http://regexlib.com/REDetails.aspx?regexp_id=1121

Avatar of curiouswebster

ASKER

This one is close but no cigar.

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


I modified it to not require "http:" or "https:" and maybe I broke it.

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

Then this URL passes but it should fail:
~/Content/images/PHOTO1.jpg

Any ideas?
You have to convert a relative url to absolutive url.
Try to use Page.ResolveUrl("~/Content/images/PHOTO1.jpg") before you validate it.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!