module Rubymisc::Regex

Public Class Methods

date_iso_8601() click to toggle source
# File lib/rubymisc/regex.rb, line 59
def date_iso_8601
  /\A(?<year>\d{4})-(?<month>(0\d|1[0-2]))-(?<day>([0-2]\d|3[01]))\z/
end
email() click to toggle source
# File lib/rubymisc/regex.rb, line 8
def email
  email_name_regex  = '[\w\.%\+\-]+'.freeze
  domain_head_regex = '(?:[A-Z0-9\-]+\.)+'.freeze
  domain_tld_regex  = '(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)'.freeze
  /\A#{email_name_regex}@#{domain_head_regex}#{domain_tld_regex}\z/i
end
hexcode() click to toggle source
# File lib/rubymisc/regex.rb, line 51
def hexcode
  /\A#(\h{3}){1,2}\z/
end
ipv4() click to toggle source
# File lib/rubymisc/regex.rb, line 42
def ipv4
  ip_octet = '(\d|[01]?\d\d|2[0-4]\d|25[0-5])'.freeze
  /\A#{ip_octet}\.#{ip_octet}\.#{ip_octet}\.#{ip_octet}\z/
end
mac_address() click to toggle source
# File lib/rubymisc/regex.rb, line 47
def mac_address
  /\A(\h{2}:){5}\h{2}\z/
end
negation(str) click to toggle source

anything that does not contain @str

# File lib/rubymisc/regex.rb, line 69
def negation(str)
  /^(?!.*#{Regexp.escape(str)}).+$/
end
phone() click to toggle source
# File lib/rubymisc/regex.rb, line 15
def phone
  /\A
    (?:
      (?<prefix>\d)             # prefix digit
      [ \-\.]?                  # optional separator
    )?
    (?:
      \(?(?<areacode>\d{3})\)?  # area code
      [ \-\.]                   # separator
    )?
    (?<trunk>\d{3})             # trunk
    [ \-\.]                     # separator
    (?<line>\d{4})              # line
    (?:\ ?x?                    # optional space or 'x'
      (?<extension>\d+)         # extension
    )?
  \z/x
end
strong_password() click to toggle source

6+ letter password w. at least 1 number, 1 letter, 1 symbol

# File lib/rubymisc/regex.rb, line 64
def strong_password
  /^(?=.*\d)(?=.*[a-z])(?=.*[\W_]).{6,}$/i
end
url() click to toggle source
# File lib/rubymisc/regex.rb, line 34
def url
  /\A((https?):(([A-Za-z0-9$_.+!*(),;\/?:@&~=-])|%[A-Za-z0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;\/?:@&~=%-]*))?([A-Za-z0-9$_+!*();\/?:~-]))\z/
end
usd() click to toggle source
# File lib/rubymisc/regex.rb, line 55
def usd
  /\A\$(\d{1,3}(\,\d{3})*|\d+)(\.\d{2})?\z/
end
zip() click to toggle source
# File lib/rubymisc/regex.rb, line 38
def zip
  /\A\d{5}(?:-\d{4})?\z/
end