Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for email address

Tags:

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]

like image 783
Logan S. Avatar asked Aug 01 '12 13:08

Logan S.


People also ask

What is the regex for email?

[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times. @ matches itself.

How do I validate an email address in regex?

Additionally, the second string needs to contain a dot, which has an additional 2-3 characters after that. With that in mind, to generally validate an email address in JavaScript via Regular Expressions, we translate the rough sketch into a RegExp : let regex = new RegExp('[a-z0-9]+@[a-z]+\.

What is simplest regular expression for email validation?

Regex : ^(.+)@(.+)$ This one is simplest and only cares about '@' symbol. Before and after '@' symbol, there can be any number of characters. Let's see a quick example to see what I mean.


2 Answers

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}

like image 75
Ryan McDonough Avatar answered Oct 24 '22 10:10

Ryan McDonough


NSString *string= @"[email protected] [email protected] [email protected]"; NSError *error = NULL; NSRegularExpression *regex = nil; regex = [NSRegularExpression regularExpressionWithPattern:@"\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,6}"                                                   options:NSRegularExpressionCaseInsensitive                                                     error:&error];    NSUInteger numberOfMatches = 0; numberOfMatches = [regex numberOfMatchesInString:string                                          options:0                                            range:NSMakeRange(0, [string length])]; NSLog(@"numberOfMatches is: %lu", numberOfMatches); 

There are more options of course. Read here.

like image 41
Avi Cohen Avatar answered Oct 24 '22 10:10

Avi Cohen