curiouswebster
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.jp g
Any ideas?
return Regex.IsMatch(url, @"(http|https)://([\w-]+\.
I modified it to not require "http:" or "https:" and maybe I broke it.
return Regex.IsMatch(url, @"(http|https)://([\w-]+\.
Regex.IsMatch(url, @"([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
Then this URL passes but it should fail:
~/Content/images/PHOTO1.jp
Any ideas?
You have to convert a relative url to absolutive url.
Try to use Page.ResolveUrl("~/Content /images/PH OTO1.jpg") before you validate it.
Try to use Page.ResolveUrl("~/Content
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
http://regexlib.com/REDetails.aspx?regexp_id=1121