using System.Text.RegularExpressions;
private static readonly Regex _newEmailRegex =
new Regex(@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$");
private static readonly Regex _oldEmailRegex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
private static string[] shouldPass =
"anjohans@microsoft.com",
"anders.sewerin@mac.com",
"eric.shmit@rred.cross.org"
private static string[] shouldFail =
"anjohans microsoft.com",
"anders.sewerin mac.com",
"eric.shmit rred.cross.org",
"eric.shmit@rredcrossorg",
"anders.sewerin@mac com",
"eric.shmit@rred cross.org",
public static void Main()
CheckReturns(true, shouldPass);
CheckReturns(false, shouldFail);
private static void CheckReturns(bool expected, string[] cases)
var newRegex = IsEmailAddressNewRegex(s);
var oldRegex = IsEmailAddressOldRegex(s);
var fromClass = IsEmailAddressMailClass(s);
if (newRegex != expected || oldRegex != expected || fromClass != expected)
System.Console.WriteLine("For test string '{0}' - Expected {1}, got newRegex: {2}, oldRegex: {3}, fromClass: {4}", s, expected, newRegex, oldRegex, fromClass);
private static bool IsEmailAddressNewRegex(string name)
return _newEmailRegex.Match(name).Success;
private static bool IsEmailAddressOldRegex(string name)
return _oldEmailRegex.Match(name).Success;
private static bool IsEmailAddressMailClass(string name)