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

Snippets has posted 5883 posts at DZone. View Full User Profile

URL Regex

03.10.2007
| 22104 views |
  • submit to reddit
        // 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

OK so to sum up this one should be right : if url =~ /^ https?:\/\/ [a-z\d]+(\-|\.[a-z\d]+)* \.[a-z]{2,6} ((:\d{1,5})?\/.*)? $/ix Shall the port number containt more than one digit? Should we give a limit of subdomains?

Snippets Manager replied on Fri, 2011/07/22 - 5:51pm

Sorry but I don't know either how to delete comments or write this properly :o if url =~ /^ https?:\/\/ [a-z\d]+([\-\.][a-z\d]+)*\.[a-z]{2,6} ((:\d{1,5})?\/[^\#]*(\#.*)?)? $/ix

Snippets Manager replied on Fri, 2011/07/22 - 5:51pm

/^https?:\/\/[a-z\d]+([\-\.][a-z\d]+)*\.[a-z]{2,6}((:\d{1,5})?\/[^#]*(#.*)?)?$/ix

Snippets Manager replied on Fri, 2011/07/22 - 5:51pm

Final and tested version | finally =D 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 end

Marco Montes replied on Fri, 2009/03/13 - 4:43pm

a 6 letter domain extension can be .museum

Snippets Manager replied on Thu, 2007/07/12 - 3:17pm

I believe there is a mistake in the regex when matching urls that contain a port number. this should fix it: ^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix also what is the 5 letter domain extension you are including in the match?