我正在尝试验证 Objective-C 中的名字。请注意,名称可以包含空格且只能包含字母。
这里我的代码不起作用:
NSString *nameRegex = @"^[A-Z][a-z]*[\\s{L}\\s{M}\\s{Nl}][A-Z][a-z]*$";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
BOOL isValid = [nameTest evaluateWithObject:name];
return isValid;
最佳答案
我明白了:
NSString *nameRegex = @"[a-zA-z]+([ '-][a-zA-Z]+)*$";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
BOOL isValid = [nameTest evaluateWithObject:name];
return isValid;
关于ios - 如何验证名字 Objective-C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19925246/