Regex for email address

Logan S. picture Logan S. · Aug 1, 2012 · Viewed 15.5k times · Source

Can someone show me a sample RegEx for an email address and how to use in in Objective-C?

Looking for something that fits: [email protected]

Answer

Ryan McDonough picture Ryan McDonough · Aug 1, 2012

As featured on: http://github.com/benofsky/DHValidation

- (BOOL) validateEmail: (NSString *) candidate {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 

    return [emailTest evaluateWithObject:candidate];
}

Or add the http://regexkit.sourceforge.net/RegexKitLite/ to your project and do the regex yourself like above.

Also to reply to the issue of checking if an e-mail address is actually real, not just works with regex you could use some service like: http://verify-email.org/register/levels.html

Please note this answer doesn't take into account the newer length TLDs such as .online; to include those update the final {2,4} to be {2,63}