前端之家收集整理的这篇文章主要介绍了
谓词,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
谓词是基于路径的,主要的功能是用来查询和过滤
谓词通常用到的正则表达式
1.字符类型
. 任意字符
[] 可以在字符串中限定字符的范围
\d [0-9] 数字
\D [^0-9] 非数字
\s 所有不可见字符(空格、tab)
\S 所有可见字符
\w [0-9a-zA-Z_] 单词(数字、字母、下划线)
\W [^0-9a-zA-Z] 非单词
? 前面的一个字符或者()至多出现1次 (0次或1次)
+ 前面的一个字符或者()至少出现1次 (1次或者多次)
* 前面的一个字符或者()可以出现任意次数 (0次、1次或者多次)
{n} 前面的一个字符或者()出现n次
{n,} 前面的一个字符或者()至少出现n次
{n,m} 前面的一个字符或者()出现n到m次
3.其他字符
^ 以什么开始 ^A表示以A开始
$ 以什么结束 B$表示以B结束
| 表示选择 gray|grey表示gray或者grey
() 可以改变优先级,作为一个整体
NSArray *numberArr = @[@21,@324,@234,@433,@5454];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF <= 345"];
NSArray *numberRes = [numberArr filteredArrayUsingPredicate:predicate];
NSLog(@"%@",numberRes);
NSArray *personArray = @[
[QYPerson personWithName:@"zhangsan" identify:@"xxx" andAge:18],[QYPerson personWithName:@"lisi" identify:@"yyy" andAge:17],[QYPerson personWithName:@"lisi" identify:@"yyx" andAge:19],[QYPerson personWithName:@"wangwu" identify:@"dfd" andAge:20],[QYPerson personWithName:@"Zhaoliu" identify:@"fdf" andAge:32],[QYPerson personWithName:@"tiAnqi" identify:@"kkl" andAge:27]
];
predicate = [NSPredicate predicateWithFormat:@"name == 'lisi' or age > 18"];
NSArray *personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"the array is:%@",personRes);
predicate = [NSPredicate predicateWithFormat:@"ALL age > 16"];
BOOL result = [predicate evaluateWithObject:personArray];
if (result) {
NSLog(@"All of person's age are greater than 16");
}else {
NSLog(@"Not Ok");
}
predicate = [NSPredicate predicateWithFormat:@"ANY age > 30"];
if ([predicate evaluateWithObject:personArray]) {
NSLog(@"至少有一个年龄大于30");
}else{
NSLog(@"年龄没有大于30的");
}
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] 'zh' "];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"BeginSwith after :%@",personRes);
predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'u'"];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[l] 'i'"];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
predicate =[NSPredicate predicateWithFormat:@"age IN {17,20}"];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
predicate = [NSPredicate predicateWithFormat:@"age BETWEEN {17,27}"];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
NSString *keyPath = @"name";
NSString *value = @"zhangsan";
predicate = [NSPredicate predicateWithFormat:@"%K == %@",keyPath,value];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];
NSString *name = @"lisi";
NSDictionary *varDict = @{@"NAME":name};
predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
predicateTemplate = [NSPredicate predicateWithFormat:@"age BETWEEN $AGE"];
NSNumber *beginAge = @17;
NSNumber *endAge = @30;
varDict = @{@"AGE":@[beginAge,endAge]};
predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
predicate = [NSPredicate predicateWithFormat:@"name like[cd] '??a*'"];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
NSString *regex = @"^w.*u$";
predicate = [NSPredicate predicateWithFormat:@"name matches %@",regex];
personRes = [personArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@",personRes);
}
return 0;
}
原文链接:https://www.f2er.com/regex/360138.html