我是Objective-C的初级程序员。 当我使用NSPredicate类从字典中提取字符串时,实际情况如下。 请帮我找到插入2个字符串对象的正则表达式,其中必须有一个非零的数字。
示例:
prefixPattern = @"2.";
suffixPattern = @".0.0.0";
然后finalPattern必须像@"%@SingleDigitRequiredHereWhichIsNotZero%@",prefixPattern,suffixPattern
搜索结果应该像2.1.0.0.0,2.2.0.0.0 但不能像2.0.0.0.0
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"header beginswith[c] %@",prefixPattern];
searchedHeader = [_jsonDataArray filteredArrayUsingPredicate:predicate];
//Filter 2
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"header endswith[c] %@",suffixPattern];
searchedHeader2 = [searchedHeader filteredArrayUsingPredicate:predicate2];
//Filter 3
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"header matches '[%@][1-9][%@]'",prefixPattern,suffixPattern];
//In the above line I m trying to exclude zero. Here I want only a single digit which is not zero.
searchedHeader3 = [searchedHeader filteredArrayUsingPredicate:predicate3];
NSLog(@"%@",[searchedHeader3 description]);
答案 0 :(得分:0)
试试这个,它将模式合并在一个单独的行中
NSString *prefixPattern = @"2";
NSString *suffixPattern = @"0.0.0";
NSString *pattern = [NSString stringWithFormat:@"%@.[1-9].%@", prefixPattern, suffixPattern];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"header matches %@", pattern];
searchedHeader2 = [searchedHeader filteredArrayUsingPredicate:predicate];