class UrlRegex private def self.country_code_tlds [ "ac", "ad", "ae", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "as", "at", "au", "aw", "az", "ax", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cs", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "um", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "yu", "za", "zm", "zw" ] end def self.generic_tlds [ "aero", "biz", "com", "coop", "info", "museum", "name", "net", "org", "pro", "travel", "gov", "edu", "mil", "int" ] end def self.all_tlds all_tlds = [] all_tlds = all_tlds + generic_tlds all_tlds = all_tlds + country_code_tlds return all_tlds.collect {|tld| "\\." + tld } end def self.exact_public_url return Regexp.compile("^" + loose_public_url.source + "$") end def self.exact_private_url return Regexp.compile("^" + loose_private_url.source + "$") end def self.scheme_regex "((http://)|(https://)|(ftp://))" end def self.auth_regex "(([a-zA-Z0-9]+)(:[a-zA-Z0-9]*)?@)?" end def self.domain_regex domain_part = "([a-zA-Z0-9_]+)" domain_part = "(" + domain_part + "(\\-" + domain_part + ")?)" return domain_part + "(\\." + domain_part + ")*" end def self.port_regex "(:[1-9][0-9]{0,4})?" end def self.abspath_regex "((/)|((/[a-zA-Z0-9_\\-\\.~]+(/)?)*))?" end def self.query_regex "(\\?(([a-zA-Z0-9_=&/;@,!'\\-\\.\\$\\+\\(\\)\\*])|(%[a-fA-F0-9]{2}))*)?" end def self.loose_private_url return Regexp.compile(scheme_regex + auth_regex + domain_regex + port_regex + abspath_regex + query_regex) end def self.loose_public_url tld = "(" + all_tlds.join("|") + ")" return Regexp.compile(scheme_regex + auth_regex + domain_regex + tld + port_regex + abspath_regex + query_regex) end public def self.find_url(string) match = loose_public_url.match(string) || loose_private_url.match(string) return match.begin(0), match.end(0) unless match.nil? end def self.is_url(string) return (is_public_url(string) || is_private_url(string)) ? true : false end def self.is_public_url(string) return exact_public_url.match(string) ? true : false end def self.is_private_url(string) return (exact_private_url.match(string) and not exact_public_url.match(string)) end def self.contains_url(string) return !find_url(string).nil? end def self.extract_url(string) indices = find_url(string) string.slice(indices[0], indices[1]-indices[0]) unless indices.nil? end def self.extract_urls(string) urls = [] while find_url(string) urls << extract_url(string) indices = find_url(string) string = string.slice(indices[1], string.size - indices[1]) end return urls end end