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.
newbiewebSr. Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yeelokhkCommented:
0
chandrasekar1Commented:
Little code change in the above comment

           string[] URLs = {"http://www.domain1.com", "www.domain2"};
            
            foreach (var urL in URLs)
            {
                if(Regex.IsMatch(urL, @"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"))
                {
                    //write your logic here or store the matched URLs to some other array
                }                    
                
            }
 

Open in new window

0
ddayx10Commented:
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

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

newbiewebSr. Software EngineerAuthor Commented:
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?
0
yeelokhkCommented:
You have to convert a relative url to absolutive url.
Try to use Page.ResolveUrl("~/Content/images/PHOTO1.jpg") before you validate it.
0
käµfm³d 👽Commented:
Then this URL passes but it should fail:
That's because you need to bound the regex. Because there are no anchors in the regex, here's the part of your string that makes that URL show as success:

PHOTO1.jpg

Why? Because "PHOTO1." satisfies "[\w-]+\." and "jpg" satisfies "[\w-]+". Try bounding the pattern and making "http(s)://" optional like this:

^(https?://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
newbiewebSr. Software EngineerAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.