1. Home
  2. Computing & Technology
  3. Ruby

Practical Regular Expressions: Email Addresses

By Amanda & Michael Morin, About.com

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
Explore Ruby
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. Regular Expressions
  5. Practical Regular Expressions: Email Addresses

©2009 About.com, a part of The New York Times Company.

All rights reserved.