After free spacing your RE, it turns out that there are some problems...
You do not need match protocol using *
, and the last []
does not make much sense to me...(maybe you can update your question.)
My tweak is here. See online demo. http://rubular.com/r/12J7ZRo4Qx
/^(
(https?|ftp)
:\/\/ #protocol
)? #is optional
(www\.)? #optional www
[-a-z0-9]+ #place - first so it means literally
\.
[a-z]{2,4} #trailing hostname
( #match pathname
\/ #a slash is required
[-a-z0-9]+ #same as hostname
)*
/x
The x
flag stands for free-spacing, as in perl.
Anyway, matching url is quite common. My version is not good nor bullet-proofing. It's just for demonstration.
If you need more solid RE, check this http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/.
The book Mastering Regular Expression is another definitive guide