PageRenderTime 86ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/rfc822.rb

https://github.com/wireframe/email-validator
Ruby | 28 lines | 18 code | 0 blank | 10 comment | 0 complexity | 7b55c55b99b1ad588740016871346d26 MD5 | raw file
  1. # encoding: binary
  2. # RFC822 Email Address Regex
  3. # --
  4. # Originally written by Cal Henderson
  5. # c.f. http://iamcal.com/publish/articles/php/parsing_email/
  6. #
  7. # Translated to Ruby by Tim Fletcher, with changes suggested by Dan Kubb.
  8. #
  9. # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
  10. # http://creativecommons.org/licenses/by-sa/2.5/
  11. module RFC822
  12. EMAIL_ADDRESS_REGEX = begin
  13. qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
  14. dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]'
  15. atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-' +
  16. '\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+'
  17. quoted_pair = '\\x5c[\\x00-\\x7f]'
  18. domain_literal = "\\x5b(?:#{dtext}|#{quoted_pair})*\\x5d"
  19. quoted_string = "\\x22(?:#{qtext}|#{quoted_pair})*\\x22"
  20. domain_ref = atom
  21. sub_domain = "(?:#{domain_ref}|#{domain_literal})"
  22. word = "(?:#{atom}|#{quoted_string})"
  23. domain = "#{sub_domain}(?:\\x2e#{sub_domain})*"
  24. local_part = "#{word}(?:\\x2e#{word})*"
  25. addr_spec = "#{local_part}\\x40#{domain}"
  26. pattern = /\A#{addr_spec}\z/
  27. end
  28. end