[nycphp-talk] regexp for URLs (is this correct?)
Jayesh Sheth
jayeshsh at ceruleansky.com
Mon May 3 14:30:48 EDT 2004
Hello all,
I came up with this Perl-style regular expression to validate URLs.
For example, if I want to match "http://www.google.com"
then I would test it against the following pattern:
#^([a-z]{3,}://)(([0-9a-z-]+\.)+[0-9a-z]{2,4})$#i
maybe I want to do something like this though:
#^([a-z]{3,5}://)(([0-9a-z-]+\.)+[0-9a-z]{2,4})$#i
so that
http://www.google.com
and
ftp://ftp.mozilla.org
and
https://www.amazon.com
are matched
but
httpsabc://www.somewhere.com
is not matched.
I primarily want to validate http:// links, though.
Another thought: I want to be flexible and allow the user to enter
something like:
www.google.com
Should I first try to match against:
#^([a-z]{3,5}://)(([0-9a-z-]+\.)+[0-9a-z]{2,4})$#i
and then, if that fails against:
#^(([0-9a-z-]+\.)+[0-9a-z]{2,4})$#i
I also realize that something like
http://www.google.com/
will not be matched.
Any suggestions or corrections?
Thanks in advance!
Best Regards,
- Jay
More information about the talk
mailing list