URL Regular Expression

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

Paul is the COO of kwiboo ltd and has more than 20 years 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.

04/Apr/2007 20:50 PM

Aung said:

Thanks

10/Oct/2007 07:56 AM

Shadi Abu Hilal said:

Thanks man

06/Nov/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:#%/;$()~_?\\-=\\\.&]*)"

14/Jul/2008 17:41 PM

Michael said:

Don't use the regex from Stephen G.

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

24/Aug/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.

24/Oct/2008 08:54 AM

Yahya said:

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

Thnks for presentation of bestandeveruseful site

25/Oct/2008 07: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_!~*'().;?&=$,%#-])/?)$

21/Nov/2008 13:36 PM

Sameer said:

Thanks...

07/May/2009 08: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

22/May/2009 16:43 PM

KS said:

Thanks a lot man....

12/Aug/2009 06: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

01/Oct/2009 06: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

01/Oct/2009 06:21 AM

Andone said:

thanks!

13/Nov/2009 05:47 AM

asp said:

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

16/Feb/2010 11:16 AM

amit jha said:

great! great!! great!!!

07/Apr/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..

08/Sep/2010 13:59 PM

Hiren said:

Hello sir, some times it includes

</a

when we find matches.

11/Sep/2010 05:45 AM

rahman said:

thank you paul

03/Oct/2010 14:01 PM

simon said:

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

12/Apr/2011 13:06 PM

Junaid said:

ok.... good it is

17/May/2011 15:39 PM

Alex Barac said:

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

31/May/2011 08:12 AM

http://watchavideo.NET said:

thank you, was looking for this for my site

28/Dec/2011 08:10 AM

mayasakthi said:

Thanks good

06/Mar/2012 07:12 AM

novel said:

what is the example of http regular expression

28/Aug/2012 06:40 AM

Add Comment

Name
Comment
 

Your comment has been received and will be shown once it passes moderation.