DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
URL Regex
// description of your code here
A regular expression pattern that validates a URL string, either HTTP or HTTPS.
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
You can easily use it with validates_format_of in your Model... For example, In a comment model, to check the :SiteURL on the comment creation:
class Comment < ActiveRecord::Base
validates_format_of :SiteURL, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix, :on => :create
end






Comments
Snippets Manager replied on Fri, 2011/07/22 - 5:51pm
Snippets Manager replied on Fri, 2011/07/22 - 5:51pm
if url =~ /^ https?:\/\/ [a-z\d]+([\-\.][a-z\d]+)*\.[a-z]{2,6} ((:\d{1,5})?\/[^\#]*(\#.*)?)? $/ixSnippets Manager replied on Fri, 2011/07/22 - 5:51pm
Snippets Manager replied on Fri, 2011/07/22 - 5:51pm
if url =~ /^ ( (https?):\/\/ )? # 2: protocol ( [a-z\d]+([\-\.][a-z\d]+)*\.[a-z]{2,6} ) # 3: domain ( (: ( \d{1,5} ) # 7: port )? ( \/.* )? # 8: query )? $/ix url.insert 0, "http#{'s' if $7 == '81'}://" unless $1 domain = $3 port = $7 ? $7.to_i : url.start_with?('https') ? 81 : 80 query = $8 || '' raise [url, domain, port, query].inspect endMarco Montes replied on Fri, 2009/03/13 - 4:43pm
Snippets Manager replied on Thu, 2007/07/12 - 3:17pm
^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ixalso what is the 5 letter domain extension you are including in the match?