ios - 如何在正则表达式中添加 ","以在 objective-c 中发送多封邮件

标签 ios objective-c regex xcode

<分区>

我是 iOS 新手,我遇到了有关在正则表达式中添加 , 的问题。

我的代码是这样的

 NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,100}";        
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];

如果我在正则表达式应用程序中添加 , 会崩溃

 NSString *emailRegEx = @"[A-Z0-9a-z._%+-,]+@[A-Za-z0-9.-,]+\\.[A-Za-z,]{2,100}";
 NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];

错误是这样的

NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_REGEX_INVALID_RANGE (string abc@gmail.com,xyz@gmail.com, pattern [A-Z0-9a-z._%+-,]+@[A-Za-z0-9.-,]+.[A-Za-z,]{2,100}, case 0, canon 0)'

有人遇到过这类问题吗?我需要发送多封电子邮件。 提前致谢!

最佳答案

为什么不简单地使用 , 来分隔字符串并比较每个字符串是否是有效的电子邮件。

NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
NSArray *emails = [emailField.text componentsSeparatedByString:@","];
NSMutableArray *valiedEmails = [[NSMutableArray alloc] init];
for (NSString *email in emails) {
    if ([emailTest evaluateWithObject:email]) {
        [valiedEmails addObject: email];            
    }
}
if (valiedEmails.count > 0) {
    txtviewemailaddress.text = [valiedEmails componentsJoinedByString:@", "];
    [self serverconnectionPost];
} 
else {
    //show alert that email is not valid 
}

关于ios - 如何在正则表达式中添加 ","以在 objective-c 中发送多封邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44194483/

相关文章:

javascript - Node.js 中的条件正则表达式替换

php - 用于解析斜体文本的正则表达式?

iOSOpenDev命令行工具构建报错

ios - 如何在Swift中从关联数组获取UIColor

ios - 如何为我的自定义 UICollectionViewCell 设置属性

ios - URLSessionDidFinishEventsForBackgroundURLSession 不调用 - Objective-C

javascript - 正则表达式:使用javascript测试最后一个单词后的url中是否存在最后一个斜杠?

android - iOS 和 Android 应用程序的最大大小

ios - "Bad Realm file header (#1)"iOS打开Realm文件异常

ios - 如何观察NSMutableArray中单个元素的变化?