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]
[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.
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]+\.
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.
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}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With