Skip to content

Instantly share code, notes, and snippets.

@sborsje
Created August 25, 2010 11:25

Revisions

  1. sborsje created this gist Aug 25, 2010.
    11 changes: 11 additions & 0 deletions URL-regex.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    IPv4_PART = /\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]/ # 0-255
    REGEXP = %r{
    \A
    https?:// # http:// or https://
    ([^\s:@]+:[^\s:@]*@)? # optional username:pw@
    ( (xn--)?[^\W_]+([-.][^\W_]+)*\.[a-z]{2,6}\.? | # domain (including Punycode/IDN)...
    #{IPv4_PART}(\.#{IPv4_PART}){3} ) # or IPv4
    (:\d{1,5})? # optional port
    ([/?]\S*)? # optional /whatever or ?whatever
    \Z
    }iux