You are not Logged in
Would you like to Login or Register

Today is: 05 February 2012
Check this months hot topics

URL Regular Expression

string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)"
 
kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : 26 July 2006

Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services. Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

Timoteo said:

Terrific. Thanks.

April 04, 2007 - 8:50 PM

Aung said:

Thanks

October 10, 2007 - 7:56 AM

Shadi Abu Hilal said:

Thanks man ;)

November 06, 2007 - 10:07 AM

Stephen G. said:

Excellent, this is a great help.

One suggestion though... be careful to escape the dash "-" character when using it in character class definitions []. Since a raw "-" is used to define a range (e.g., [A-Z]), you may want to escape the dash used in your Reg Ex.

The class [\w\d:#@%/;$()~_?\-=\\\.&] will unintentionally match all of the characters between and =. The following includes an escaped dash:

string pattern = "((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))[\w\d:#%/;$()~_?\\-=\\\.&]*)"

July 14, 2008 - 5:41 PM

Michael said:

Don't use the regex from Stephen G.

Url's with "-" are splitted with this code.

August 24, 2008 - 10:56 AM

Peter said:

I thing it is not good, because when you specify url like this:

http://www.google.com:xxx (where :xxx represents port number) this must failed because xxx is not number (for port), but using this regex returns true.

October 24, 2008 - 8:54 AM

Yahya said:

Hellow Admin!!!!!!!!!!!

Thnks for presentation of bestandeveruseful site

October 25, 2008 - 7:26 AM

Bonzo said:

This Regex work fine for me:

^(https?://)?(([0-9a-z_!'().&=$%-]: )?[0-9a-z_!'().&=$%-]@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!'()-]\.)([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!*'().;?:@&=$,%#-])/?)$

November 21, 2008 - 1:36 PM

Sameer said:

Thanks...

May 07, 2009 - 8:26 AM

Ryan Timoney said:

Kiiind of obscure but a note to anyone using this with cURL: There's a security hole in versions 5.11 to and including 7.19.3 where a 'file' URL can be exploited to manipulate server files and run commands as the webuser. http://curl.haxx.se/docs/adv_20090303.html

May 22, 2009 - 4:43 PM

KS said:

Thanks a lot man....

August 12, 2009 - 6:28 AM

said:

"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))[\w\d:#%/;$()~_?\-=\\\.&]*)"

if this right then thanks otherwise write in detailed not in short form

October 01, 2009 - 6:05 AM

anirudha mathur said:

"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))[\w\d:#%/;$()~_?\-=\\\.&]*)"

update your blog or write in detail not in short

blah nlah blah

October 01, 2009 - 6:21 AM

Andone said:

thanks!

November 13, 2009 - 5:47 AM

asp said:

<i>...and</i> for the url starting with <em>"/site.php?......"</em> ?

February 16, 2010 - 11:16 AM

amit jha said:

great! great!! great!!!

April 07, 2010 - 10:32 AM

mapemape said:

Dude,

I put that regular expression in my code and when I put "http://www." (without double quotes) is valid based on that regular expression. It shouldn't be valid..

September 08, 2010 - 1:59 PM

Hiren said:

Hello sir, some times it includes

</a

when we find matches.

September 11, 2010 - 5:45 AM

rahman said:

thank you paul

October 03, 2010 - 2:01 PM

simon said:

Why not use [^//s] (non blank character) ?

April 12, 2011 - 1:06 PM

Junaid said:

ok.... good it is

May 17, 2011 - 3:39 PM

Alex Barac said:

This REGEX validates a link like "http://www.asdf", I'm sure this is not supposed to happen...

May 31, 2011 - 8:12 AM

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views

More Publications

URL Regular Expression
Paul Hayman - 26/07/2006
IP Address regular expression
Paul Hayman - 04/08/2006
IsGuid() (Regular Expression Guid Match)
Paul Hayman - 14/06/2006
Date Regular Expression (dd/MMM/yyyy)
Paul Hayman - 26/10/2007
Hex code Regular Expression (#F0F0F0)
Paul Hayman - 12/10/2007