1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Practical Regular Expressions: Email Addresses

By

Though it would take an extremely large and complicated full regular expression to match every single possible email address according to RFC 5322, as demonstrated here, it's not nearly as difficult to create a simple regular expression that will match almost every email address actually in use.

Email address usernames must begin and end with a number, letter or underscore, but the middle characters can be any combination of numbers, letters, underscores, the period (.) character and the plus (+) character. Additionally, the server portion of the email address must be a valid hostname.

Note that this regex will only give you a clue as to whether the format of the address is correct. There may be some addresses that are valid, but fail with this regex. This regex also says nothing about whether the actual email address is in use and whether or not mail can be sent to it.

 #!/usr/bin/env ruby
 
 # Test cases
 emails = %w{
   email@example.com
   email.user@example.com
   email+user@example.com
   e_mail@example.com
   +email@example.com
   .email@example.com
   email@example###domain
 }
 
 email_regex = %r{
   ^ # Start of string
 
   [0-9a-z] # First character
   [0-9a-z.+]+ # Middle characters
   [0-9a-z] # Last character
 
   @ # Separating @ character
 
   [0-9a-z] # Domain name begin
   [0-9a-z.-]+ # Domain name middle
   [0-9a-z] # Domain name end
 
   $ # End of string
 }xi # Case insensitive
 
 emails.each do|e|
   if (e =~ email_regex) == 0
     puts "#{e} GOOD"
   else
     puts "#{e} BAD"
   end
 end
 
  1. About.com
  2. Technology
  3. Ruby
  4. Regular Expressions
  5. Practical Regular Expressions: Email Addresses

©2014 About.com. All rights reserved.