There was an old email thread from the end of January 2006 discussing
regular expressions for matching URLs. I took some of the sample
expressions and tweaked them a bit to the point where I think they
are pretty useful. They pass all of my tests anyway.
For a reminder, here’s a sample message from that thread. My code is
below it.
On Jan 26, 2006, at 5:28 AM, Nathaniel S. H. Brown wrote:
> Subject: Re: [Rails] [ANN] HTTP URL validation plugin >> > http://username:[email protected]/?q=r&me=you#some_id > > Sorry, my REGEX skills are quite poor.. > > Jeroen
Nathaniel’s expression almost worked. It didn’t match properly on
URLs that contained a port designation and it failed to match if a
URL didn’t have ‘.com’ or similar on the end of the host (like http://
localhost). Here’s an expression that’s working pretty well for me.
Thanks to Nathaniel’s work, and others, it didn’t take me as much
effort as I feared.
/^([A-Za-z.±]+)://([a-z0-9]+:[a-z0-9]+@)?[a-z0-9]+([-.]{1}[a-
z0-9]+)(.[a-z]{2,5})?(([:0-9]{1,5})?/.)?$/ix
I hope this is useful to somebody. If you make any further
improvements, please post them back to the list!
cr