Email Regular expression issue while using special characters [duplicate]

梦想的初衷 提交于 2019-12-13 08:55:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!