正規表現でメールをチェックするのをやめてください!

真剣に、やめてください。 これは時間と労力の無駄です。 Googleで定期的なメールチェックを探し、それを見てください。新鮮な空気を吸いに外に出たいと思うでしょう。 私は非常に有名な引用を思い出します:



問題に直面したとき、「ああ、正規表現を使用します」と考える人もいます。

現在、2つの問題があります。



ジェイミー・ザウィンスキーregex.info


これは、Railsアプリケーションのかなり一般的なサンプルコードです。承認システムの外観が含まれています。



class User < ActiveRecord::Base
  #      from https://github.com/plataformatec/devise,
  #      Rails
  validates_format_of :email, :with => /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/
end

      
      





( ), :



class User < ActiveRecord::Base
  validates_format_of :email, :with => /^(|(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6})$/i
end

      
      





:



class User < ActiveRecord::Base
  validates :email, :with => EmailAddressValidator
end

class EmailValidator < ActiveModel::Validator
  EMAIL_ADDRESS_QTEXT           = Regexp.new '[^\\x0d\\x22\\x5c\\x80-\\xff]', nil, 'n'
  EMAIL_ADDRESS_DTEXT           = Regexp.new '[^\\x0d\\x5b-\\x5d\\x80-\\xff]', nil, 'n'
  EMAIL_ADDRESS_ATOM            = Regexp.new '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+', nil, 'n'
  EMAIL_ADDRESS_QUOTED_PAIR     = Regexp.new '\\x5c[\\x00-\\x7f]', nil, 'n'
  EMAIL_ADDRESS_DOMAIN_LITERAL  = Regexp.new "\\x5b(?:#{EMAIL_ADDRESS_DTEXT}|#{EMAIL_ADDRESS_QUOTED_PAIR})*\\x5d", nil, 'n'
  EMAIL_ADDRESS_QUOTED_STRING   = Regexp.new "\\x22(?:#{EMAIL_ADDRESS_QTEXT}|#{EMAIL_ADDRESS_QUOTED_PAIR})*\\x22", nil, 'n'
  EMAIL_ADDRESS_DOMAIN_REF      = EMAIL_ADDRESS_ATOM
  EMAIL_ADDRESS_SUB_DOMAIN      = "(?:#{EMAIL_ADDRESS_DOMAIN_REF}|#{EMAIL_ADDRESS_DOMAIN_LITERAL})"
  EMAIL_ADDRESS_WORD            = "(?:#{EMAIL_ADDRESS_ATOM}|#{EMAIL_ADDRESS_QUOTED_STRING})"
  EMAIL_ADDRESS_DOMAIN          = "#{EMAIL_ADDRESS_SUB_DOMAIN}(?:\\x2e#{EMAIL_ADDRESS_SUB_DOMAIN})*"
  EMAIL_ADDRESS_LOCAL_PART      = "#{EMAIL_ADDRESS_WORD}(?:\\x2e#{EMAIL_ADDRESS_WORD})*"
  EMAIL_ADDRESS_SPEC            = "#{EMAIL_ADDRESS_LOCAL_PART}\\x40#{EMAIL_ADDRESS_DOMAIN}"
  EMAIL_ADDRESS_PATTERN         = Regexp.new "#{EMAIL_ADDRESS_SPEC}", nil, 'n'
  EMAIL_ADDRESS_EXACT_PATTERN   = Regexp.new "\\A#{EMAIL_ADDRESS_SPEC}\\z", nil, 'n'

  def validate(record)
    unless record.email =~ EMAIL_ADDRESS_EXACT_PATTERN
      record.errors[:email] << 'is invalid'
    end
  end
end

      
      





. ? , , ( ) email-, RFC. , , .



, email- , 3.2.4 3.4.1. , , . ( , @), :



! $ & * - = ^ ` | ~ # % ' + / ? _ { }


? , , . , — :



"Look at all these spaces!"@ example.com


!



, email- :



class User < ActiveRecord::Base
  validates_format_of :email, :with => /@/
end

      
      





, ? @. , . , .



, email , ? , , , .



!



, . . , . , ?



. :



qwiufaisjdbvaadsjghb@gmail.com


! , : email-! , email , . , , , . , ? — . , , . .



. , . , , . — , .



, , @. , — :



/.+@.+\..+/i


, — .



:

. jetman!



All Articles