cremes.devlist@mac.com
2006-Jun-13 22:08 UTC
[Rails] HTTP validation regexp, possible solution
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:> It only allows for URL''s without the HTTP AUTH syntax you mentioned. > > /^(http|https|ftp|smb):\/\/(a-z0-9]+:[a-z0-9]+@)?[a-z0-9]+([\-\.]{1} > [a-z0-9] > +)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix > > Without testing, the above should work in such a case. > > -Nb > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Nathaniel S. H. Brown nshb.net > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > >> -----Original Message----- >> From: rails-bounces@lists.rubyonrails.org >> [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of >> Jeroen Houben >> Sent: January 26, 2006 3:04 AM >> To: rails@lists.rubyonrails.org >> Subject: Re: [Rails] [ANN] HTTP URL validation plugin >> >> Nathaniel S. H. Brown wrote: >>> One useful addition worth considering is adding this RegEX >>> as a define or even as a helper to AR in the format of >>> "validates_format_of_url :url" to be a shorthand for the REGEX. >>> >>> Source: nshb.net/node/252 >>> >>> class Company < ActiveRecord::Base >>> validates_format_of :url, :with => >>> >>> /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5} >>> (([0-9]{1 >>> ,5})?\/.*)?$/ix >>> end >>> >> >> >> Technically a URL can also start with ftp:// smb:// etc >> >> Does this also allow stuff like: >> username:password@www.example.com/?q=r&me=you#some_id >> >> Sorry, my REGEX skills are quite poor.. >> >> JeroenNathaniel''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