A Good URL Regular Expression

(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|
file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%
[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;
/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))

Regular Expressions are magical. Up until now I always discarded them as being to “geeky”, meaning I don’t consider it my life’s biggest goal to be typing (/?[]\w) all day long.

Regular expression that validates URLs not URL domains. One that doesn’t allow spaces in the domain name and where the domain can be suffixed with the port number. It also supports ~/ paths, and sub folders.

Should parse the URL below:
http://hh-1hallo.msn.blabla.com:80800/test/test/test.aspx?dd=dd&id=dki

But not:
http://hh-1hallo. msn.blablabla.com:80800/test/test.aspx?dd=dd&id=dki

I hope that this helps people.

Leave a Reply

Your email address will not be published. Required fields are marked *