我在Core Data Entity
添加了电子邮件验证,如图:
但是无法验证。错误是这样的:
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.)}
我的正则表达式有问题吗?
答案 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];