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

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find URL Test
Expression
(?#WebOrIP)((?#protocol)((http|https):\/\/)?(?#subDomain)(([a-zA-Z0-9]+\.(?#domain)[a-zA-Z0-9\-]+(?#TLD)(\.[a-zA-Z]+){1,2})|(?#IPAddress)((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])))+(?#Port)(:[1-9][0-9]*)?)+(?#Path)((\/((?#dirOrFileName)[a-zA-Z0-9_\-\%\~\+]+)?)*)?(?#extension)(\.([a-zA-Z0-9_]+))?(?#parameters)(\?([a-zA-Z0-9_\-]+\=[a-z-A-Z0-9_\-\%\~\+]+)?(?#additionalParameters)(\&([a-zA-Z0-9_\-]+\=[a-z-A-Z0-9_\-\%\~\+]+)?)*)?
Description
I needed a regex for validating URLs, couldn't find a suitable one, so wrote this, with comments! It matches any valid web URL (Address or IP, with or without protocol), including optional port number, directory path, filname.extension and any paramater pairs. Hope it helps, even if just to understand expressions easier!
Matches
www.test.com|http://www.test.com|234.232.12.23:8080|sub.test.com/dir/file.ext?param=val&param2=val2
Non-Matches
/file.htm|256.0.0.0
Author Rating: The rating for this expression. David Barker
Source http://www.barkerd.co.uk/regex.htm
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: Mr.
Name: Dumbhead
Date: 8/31/2007 3:51:22 PM
Comment:
It doesn't work in cases where there's nothing after an "=" in the additional parameters e.g. for the below: http://mysite.com/software//detect.asp?SomeVal= it matches: http://mysite.com/software//detect.asp? Is there a way to include "SomeVal=" in the expression?


Title: Mr.
Name: Dumbhead
Date: 8/31/2007 3:46:06 PM
Comment:
It doesn't work in cases where there's nothing after an "=" in the additional parameters e.g. for the below: http://mysite.com/software//detect.asp?SomeVal= it matches: http://mysite.com/software//detect.asp? Is there a way to include "SomeVal=" in the expression?


Title: Note to Stian
Name: Greg Lorriman
Date: 7/27/2007 9:36:13 AM
Comment:
# is explicitly disallowed in the rfc, it must be encoded. Also your subdomain regex is wrong.


Title: mhh need of
Name: felix nagel
Date: 7/23/2007 6:45:03 PM
Comment:
you need to allow something like this: http://test.de without subdomain


Title: Great so far, but lacks 2 things
Name: Stian
Date: 12/23/2006 1:29:51 PM
Comment:
It demands a subdomain, so http://youdomain.com is not valid. Should be "(?#subDomain)(([a-zA-Z0-9]+\.*". Also, it will not accept anchors starting with #


Title: Looks good so far, but I would add...
Name: Todd Davis
Date: 10/24/2006 11:10:10 AM
Comment:
Instead of just checking for http|https, I would add all the prefix's: file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp


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