谓词

谓词是基于路径的,主要的功能是用来查询和过滤

谓词通常用到的正则表达式

1.字符类型

.  任意字符
[] 可以在字符串中限定字符的范围
\d [0-9] 数字
\D [^0-9] 非数字
\s 所有不可见字符(空格、tab)
\S 所有可见字符
\w [0-9a-zA-Z_] 单词(数字、字母、下划线)
\W [^0-9a-zA-Z] 非单词

2.数量限定

?     前面的一个字符或者()至多出现1次 (0次或1次)
+     前面的一个字符或者()至少出现1次 (1次或者多次)
*     前面的一个字符或者()可以出现任意次数 (0次、1次或者多次)
{n}   前面的一个字符或者()出现n次
{n,}  前面的一个字符或者()至少出现n次
{n,m} 前面的一个字符或者()出现n到m次

3.其他字符

^     以什么开始 ^A表示以A开始
$     以什么结束 B$表示以B结束
|     表示选择 gray|grey表示gray或者grey
()    可以改变优先级,作为一个整体
//谓词NSPredicate用来查询和过滤 类似于SQL中的where

        //1创建 number类型的数组
        NSArray *numberArr = @[@21,@324,@234,@433,@5454];

        //1.1创建谓词对象<= 345的数过滤出来(> >= <= != <)
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF <= 345"];
        //1.2通过谓词对象。来过滤数组形成新的数组
        NSArray *numberRes = [numberArr filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",numberRes);

        //2 创建一个person数组
        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]

                                 ];
        //2.1 创建一个谓词对象name == 'lisi' && age > 18 (&& and || or)
        predicate = [NSPredicate predicateWithFormat:@"name == 'lisi' or age > 18"];
        //2.2通过谓词对象过滤person数组
        NSArray *personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"the array is:%@",personRes);

        //3 ALL ANY 施加的是结合对象
        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的");
        }

        //4.指定字符串 BEGINSWITH ENDSWITH CONTAINS [cd]([cd]不区分大小写和音调)
        //4.1过滤以指定字符串开头的数据
        predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] 'zh' "];
        personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"BeginSwith after :%@",personRes);

        //4.2过滤以指定字符串结尾的数据
        predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'u'"];
        personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",personRes);

        //4.3过滤包含指定字符串的数据
        predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[l] 'i'"];
        personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",personRes);

        //5.方位运算符 IN BETWEEN
        //5.1 IN 筛选给定的离散数据
        predicate =[NSPredicate predicateWithFormat:@"age IN {17,20}"];
        personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",personRes);
        //5.2 BETWEEN 筛选给定范围内的数据
        predicate = [NSPredicate predicateWithFormat:@"age BETWEEN {17,27}"];
        personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",personRes);

        //6. 谓词中键的占位符 %K,谓词中使用变量
        //6.1 %K
        NSString *keyPath = @"name";
        NSString *value = @"zhangsan";
        predicate = [NSPredicate predicateWithFormat:@"%K == %@",keyPath,value];
        personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",personRes);

        //6.2变量($NAME)谓词模板
        //6.2.1生成一个谓词模板,这个模板中,一个变量$NAME来先占位置
        NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];
        //6.2.2 为$NAME赋值的过程,其实就是创建一个字典
        NSString *name = @"lisi";
        //该字典的键就是谓词模板中的变量名(NAME)
        NSDictionary *varDict = @{@"NAME":name};
        //6.2.3 生成最终的谓词(变量已经被真实地值替换了)
        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);

        //谓词中使用通配符 like  下面单独介绍通配符
        predicate = [NSPredicate predicateWithFormat:@"name like[cd] '??a*'"];
        personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",personRes);

        //谓词中使用正则表达式 matches 下面单独介绍正则表达式
        //名字中以w开始,以u结束的
        NSString *regex = @"^w.*u$";
        predicate = [NSPredicate predicateWithFormat:@"name matches %@",regex];
        personRes = [personArray filteredArrayUsingPredicate:predicate];
        NSLog(@"%@",personRes);
    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值