问题
I have created the following regular expression for validating my email
- (BOOL)isValidEmail
{
BOOL stricterFilter = YES;
NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
NSString *laxString = @".+@([A-Za-z0-9]+\\.)+[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
But it fails when i enter following:
b. !@#$%^ bfv@mn.cn
Is there any issue with my regular expression?
回答1:
Try this. It's work perfect for me.
NSString *mailreg=@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *mailtest=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",mailreg];
return [mailtest evaluateWithObject:checkString];
来源:https://stackoverflow.com/questions/34493418/email-regular-expression-issue-while-using-special-characters