An0r4k An0r4k - 1 year ago 117
Objective-C Question

Regex for email address

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

Looking for something that fits:


Answer Source

As featured on:

- (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 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:

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}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download