zoukankan      html  css  js  c++  java
  • 谓词

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

    谓词通经常使用到的正則表達式

    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; }

  • 相关阅读:
    sql常用语句
    java学习(东软睿道)2019-09-06(预课)《随堂笔记》
    Servlet和JSP学习总结
    由字符集的转换想到的问题
    mysql主从搭建
    CentOS 源码安装MySQL5.7
    Linux搭建FTP服务器
    连接MySQL报错误代码 ERROR 1045时的解决方案
    [js]使用百度编辑器uediter时遇到的一些问题(span,div等被过滤)
    [css]将textarea前的文字设置在左上角
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7112941.html
Copyright © 2011-2022 走看看