RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find URL Validator
Expression
^(http(?:s)?\:\/\/[a-zA-Z0-9]+(?:(?:\.|\-)[a-zA-Z0-9]+)+(?:\:\d+)?(?:\/[\w\-]+)*(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$
Description
A simple but powerful URL validating regex. Accepts multiple sub-domains and sub-directories. Even accept query strings. Now accept ports! Accepts HTTP or HTTPS. Also accepts optional "/" on end of address.
Matches
http://website.com | http://subdomain.web-site.com/cgi-bin/perl.cgi?key1=value1&key2=value2
Non-Matches
http://website.com/perl.cgi?key= | http://web-site.com/cgi-bin/perl.cgi?key1=value1&key2
Author Rating: The rating for this expression. tedcambron
Source
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: check url
Name: miki
Date: 9/28/2016 7:43:54 AM
Comment:
how check ? 1 if page name only(with out http(s) ftp(s)... + params data ? 2 page name is not English name ? 3 page name is relative path \..\page name


Title: Optional Protocol
Name: Riccardo
Date: 5/12/2016 10:03:06 AM
Comment:
In need of validate user input (asked for URL) I changed it as follow in the protocol section. Now the protocol is no more mandatory and, if added, can be one of ftp/ftps/http/https ^(?:((ht|f)tp(?:s)?\:\/\/|)[a-zA-Z0-9]+(?:(?:\.|\-)[a-zA-Z0-9]+)+(?:\:\d+)?(?:\/[\w\-]+)*(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$


Title: update
Name: Emblibrary
Date: 11/25/2015 1:58:01 AM
Comment:
Very constructive for new beginners to learn about the field that is newer for them. <b><a href="http://www.digitemb.com/emb-library.php">Emblibrary</a></b>


Title: update
Name: Emblibrary
Date: 11/25/2015 1:57:11 AM
Comment:
Very constructive for new beginners to learn about the field that is newer for them. <b><a href="http://www.digitemb.com/emb-library.php">Emblibrary</a></b>


Title: Showing vali in http://www.website
Name: AVR
Date: 7/23/2015 5:11:02 AM
Comment:
I tried with this url http://www.website and it shows valid. but this url is invalid


Title: Improved
Name: Ted Cambron
Date: 4/13/2015 9:59:02 PM
Comment:
Now accepts "http://www.site.com:8008" and "http://192.168.0.102:81/play.pl?param=2" :)


Title: URL Validator Regex
Name: Prafulla
Date: 1/23/2014 1:56:28 AM
Comment:
The problem with the above regex is, it doesnot matches though it is a valid url http://website.com/perl?key=value Also if you want to allow both http and www, then use ^(((http(?:s)?\:\/\/)|www\.)[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+((\.[a-zA-Z]{2,4})?)(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$


Title: URL Validator Regex
Name: Prafulla
Date: 1/23/2014 1:48:39 AM
Comment:
The problem with the above regex is, it doesnot matches though it is a valid url http://website.com/perl?key=value Also if you want to allow both http and www, then use ^(((http(?:s)?\:\/\/)|www\.)[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+((\.[a-zA-Z]{2,4})?)(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$


Title: URL Validation
Name: Shantonu
Date: 7/7/2013 7:26:10 AM
Comment:
(https?|ftp)://(www\d?|[a-zA-Z0-9]+)?\.[a-zA-Z0-9-]+(\:|\.)([a-zA-Z0-9.]+|(\d+)?)([/?].*)?


Title: one case failure
Name: Aditya Singh
Date: 5/28/2013 7:07:58 AM
Comment:
it does not work for this case of url "http://www.asssdd" it should be invalid but it accept as valid url


Title: one case failure
Name: Aditya Singh
Date: 5/28/2013 7:06:39 AM
Comment:
it does not work for this case of url "http://www.asssdd" it should be invalid but it accept as valid url


Title: Seven Cor
Name: hgfhgf
Date: 9/18/2012 9:06:49 PM
Comment:
Seven Core, [url=http://www.sevencoregold.com/gold.html]buy seven core gold[/url] Seven Core, <a href="http://www.sevencoregold.com/gold.html">buy seven core gold</a> a new MMORPG with riding actions as its core gameplay, is currently being developed by Noria


Title: Seven Cor
Name: hgfhgf
Date: 9/18/2012 9:06:34 PM
Comment:
Seven Core, [url=http://www.sevencoregold.com/gold.html]buy seven core gold[/url] Seven Core, <a href="http://www.sevencoregold.com/gold.html">buy seven core gold</a> a new MMORPG with riding actions as its core gameplay, is currently being developed by Noria


Title: cheap jerseys
Name: cheap jerseys
Date: 9/6/2012 11:00:16 PM
Comment:
You are so cool! I read a few words in these before, I don't think. So nice to search out some original idea on this subject. Really thank you. In fact this site is hope on the Internet, with a little creativity. Useful work to bring new Internet innovation! http://www.redbottomshoesplaza.com/ http://www.christianlouboutinfreeus.com/ http://www.cheapjerseysvipclub.com/


Title: nike air max tn sko
Name: dksko
Date: 7/24/2012 3:12:47 AM
Comment:
Nike Air Max provides the ultimate in impact protection — reducing shock and distributing pressure — to provide you with absolute comfort and stability. http://www.dksko.com/


Title: Won't match url with port
Name: Al
Date: 1/5/2011 12:12:41 PM
Comment:
For example http://www.site.com:8008 doesn't match the pattern.


Title: Relax the Regex
Name: Benno
Date: 12/9/2010 9:44:50 AM
Comment:
This fails for me on a simple url. http://192.168.0.102:81/play.pl?param=2 HTTP://SMH.COM.AU also fails - even through browsers honour it. The reality is that, all the text after the first trailing forward slash - is potentially valid in the real world. Once a web server receives a GET or POST string it has the option to parse (and make sense of) any possible ASCII text and return a valid response. It depends on your requirement - but if it is to ensure the user has entered a web address that is possibly correct - whether an intranet address, a local nextwork address or real web - then the regex should be relaxed to the maximum extent. Here is mine (in Perl) - the trailing /i means ignore case - as per browsers $url=~/^http:\/\/[a-z0-9_\-\.\:]+(|\/\S*)$/i; Cheers Ben I propose ^http:\/\/[a-z0-9_\-]+ So I propose


Title: Thanks
Name: Ted Cambron
Date: 10/8/2010 8:57:51 PM
Comment:
Thanks viren negi, Good feedback. I'll update the regex as you suggested when I get some time to sit down.


Title: Great Regex but not full proof
Name: viren negi
Date: 9/23/2009 2:16:38 AM
Comment:
I must say by far your regex is far more robust and better compare to other regex's that one can find over the INTENET in distinguishing between malformed and proper URL .But it still feel it lack some basic functionality check like 1.If a URL has "https". I know it simple one just need to append "s?" after "http" of your regex but still. 2.The regex treat a URL as proper and not malformed even if a URL contain and "_". e.g https://www._google.com is identified as proper and not malformed URL (above URL is been checked against your regex in http://rubular.com) to get rid of this I just removed all the '_' form your regex. hope you don't mind. It works fien then I wonder what are those '_' doing there. Hope that you put more light on to it in your future comment 3. the regex treat the URL http://www.google.com/ as malformed.which its shouldn't have done. rest all is fine Great work Man.Truly Great Carry on with such wonderful work and please make the necessary change in your regex for the betterment of all our fellow developers


Title: URI.pm is insecure
Name: Ted Cambron
Date: 6/9/2008 9:13:16 PM
Comment:
OK, once again for all you purists. The URI.pm family is insecure. Every hacker knows it. A double dot is a valid URI and also a good way to do a traversal hack that can wipe out a database. Using URI.pm will not prevent this. Using my regex will. Why use URI.pm and then use another piece of code to loop through the data and look for double dots when all you need is one simple regex? Thank you sir and good day.


Title: URI.pm is insecure
Name: Ted Cambron
Date: 6/9/2008 9:13:05 PM
Comment:
OK, once again for all you purists. The URI.pm family is insecure. Every hacker knows it. A double dot is a valid URI and also a good way to do a traversal hack that can wipe out a database. Using URI.pm will not prevent this. Using my regex will. Why use URI.pm and then use another piece of code to loop through the data and look for double dots when all you need is one simple regex? Thank you sir and good day.


Title: Not so hot
Name: Meno d'Emme
Date: 4/21/2008 10:41:20 PM
Comment:
The failure cases are valid URIs and there are many more valid's that the Rx will not match. The Rx will also match things that are invalid. The URI.pm family is core in Perl and does these things correctly and with more power and flexibility. This seems to be case will all the Rxes posted by this author. I.e., there is a core, or well known, module that will do it right and the Rxes in question fail on valid cases and pass certain bad ones.


Title: Not so hot
Name: Meno d'Emme
Date: 4/21/2008 10:41:05 PM
Comment:
The failure cases are valid URIs and there are many more valid's that the Rx will not match. The Rx will also match things that are invalid. The URI.pm family is core in Perl and does these things correctly and with more power and flexibility. This seems to be case will all the Rxes posted by this author. I.e., there is a core, or well known, module that will do it right and the Rxes in question fail on valid cases and pass certain bad ones.


Title: Malfunction
Name: [email protected]
Date: 4/17/2008 9:29:34 AM
Comment:
Using the URL http://regexlib.com/UserPatterns.aspx?authorId=4f1e9e8d-d9fa-4221-ac16-ee9534263d28 there is no match and also no match using the .net engine or the clients engine. Therefore I would offer: ^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_\-]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_\-]+=[a-zA-Z0-9_\-]+)?)?(?:\&[a-zA-Z0-9_\-]+\=[a-zA-Z0-9_\-]+)*)$


Title: Update
Name: Ted Cambron
Date: 7/28/2007 12:36:21 AM
Comment:
Dang, a double post. :( After further test I made one more change and it's working quite well. Here's the finished product... ^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$


Title: Update
Name: Ted Cambron
Date: 7/28/2007 12:34:11 AM
Comment:
Had to make a slight upate to the query part. ^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]?)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$


Title: Update
Name: Ted Cambron
Date: 7/28/2007 12:25:29 AM
Comment:
Had to make a slight upate to the query part. ^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]?)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials