我想只允许a-z,0-9和_(下划线),但它无法正常工作。
NSString *test = @"name_test09"; // should be valid
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"[a-z0-9_]"];
if ([predicate evaluateWithObject:test] == NO) {
// not validated
} else {
// validated
}
答案 0 :(得分:-1)
应该是。
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", @"[a-z][0-9]_"];
我的编辑:
NSString *s =@"abcdefghijklmnopqrstuvwxyz1234567890_";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@",s];
或强>
NSString *myRegex = @"[a-z0-9_]*";
NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", myRegex];
BOOL valid = [myTest evaluateWithObject: test];
if (valid)
{
}
else
{
}