0

I'm using the following regex to validate emails in an email DB of a Rails App:

/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

it kinda works, but it leaves me with invalid mails wich have this kind of errors:

name@domain..com

How can I rwrite this regex to avoid that or wich is the best regex to clean up an email list I have to leave only valid email addresses? I'm using a method like this one to clean up the list:

def mailSweep
  mails = Email.all.lazy
  for address in mails do
    if address.email.to_s =~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
      puts address.email.to_s + " " + "it's valid"
    else
      puts address.email.to_s + " " + "it's invalid, destroying..."
      address.destroy
    end
  end
end
Jmlevick
  • 6,504
  • 9
  • 29
  • 35
  • 3
    See http://stackoverflow.com/questions/703060/valid-email-address-regular-expression for the sad reality of regex-validating email addresses. Then find a gem to do it for you. Here's one: https://github.com/dim/rfc-822. – Jim Stewart Jun 09 '13 at 23:19
  • @Jim Stewart, +1 for showing both why it's flawed and an excellent alternative solution. – Ro Yo Mi Jun 10 '13 at 03:24

1 Answers1

1

I suggest

/\A[^@\s]+@([-a-zA-Z0-9]{1,63}\.)+[a-zA-Z0-9]{2,63}\z/

Note that this regex does not allow all legal email addresses. You need to something insanely complicated to allow for all RFC 822 valid email addresses that have been stripped of comments. If you want to include comments, then it is beyond the power of regular expressions and you have to go to a lexical parser.

Even then, you can't be sure the email is really valid. The best check is to try to send to it and see if the receiving system accepts it. Of course, even then it might be silently discarded....

Community
  • 1
  • 1
Old Pro
  • 24,624
  • 7
  • 58
  • 106