Regex for an email address doesn't work

后端 未结 8 2095
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 22:32

I\'m trying to check if some email address is correct with the following code :

NSPredicate *regexMail = [NSPredicate predicateWithFormat:@\"SELF MATCHES \'.*@.*         


        
8条回答
  •  醉梦人生
    2021-02-04 23:07

    Here's a working example, with a slightly more appropriate pattern (although it's not perfect, as others have mentioned):

    NSString* pattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    if ([predicate evaluateWithObject:@"johndoe@example.com"] == YES) {
      // Match
    } else {
      // No Match
    }
    

提交回复
热议问题