我想在Objective c中检查带有正则表达式的字符串。我的代码
NSString *regexAmazonOrder =@"[a-zA-Z0-9]*";
NSPredicate *predicateAmazonOrder = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexAmazonOrder];
if([predicateAmazonOrder evaluateWithObject:cardNumber.text])
{
NSLog(@"Its coming inside AMAZON ORDER EVALUATIONS");
...
}
但它不起作用。帮帮我。我知道它很简单但是吃了我的心!感谢
答案 0 :(得分:3)
这是更短的版本:
+(BOOL)isAlphanumericString:(NSString *)string
{
NSString *filter = @"[a-z0-9]*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", filter];
return [predicate evaluateWithObject:string];
}
答案 1 :(得分:2)
尝试这样:(未经测试,只是为了给你一个启动者)
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-z0-9]*"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *matches = [regex matchesInString:cardNumber.text
options:0
range:NSMakeRange(0, [string length])];
if([matches count] > 0)
{
// Valid input
}