核心数据验证在电子邮件的情况下失败

时间:2015-09-01 09:24:34

标签: ios regex core-data nsregularexpression

我在Core Data Entity添加了电子邮件验证,如图:

enter image description here

但是无法验证。错误是这样的:

NSValidationErrorValue=example@gmail.com, 
NSValidationErrorPredicate=SELF MATCHES 
    "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\\\.[A-Za-z]{2,4}",
NSValidationErrorKey=email,
NSLocalizedDescription=The operation couldn't be completed. (Cocoa error 1680.)} 

我的正则表达式有问题吗?

1 个答案:

答案 0 :(得分:0)

你的正则表达式有点偏。

试试这个

NSString *email = @"example@gmail.com";
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL result = [predicate evaluateWithObject:email];