<a target="_blank" href="http://blog.csdn.net/z251257144/article/details/8576308">原文:http://www.2cto.com/kf/201208/150608.html</a>
<a target="_blank" href="http://blog.csdn.net/iscape/article/details/7318021">参考:http://blog.csdn.net/iscape/article/details/7318021</a>
<a target="_blank" href="http://blog.csdn.net/zhulei1018/article/details/6777220">参考:http://blog.csdn.net/zhulei1018/article/details/6777220</a>
首先举一个例子:
匹配9-15个由字母/数字组成的字符串的正则表达式:
nsstring * regex = @"^[a-za-z0-9]{9,15}$";
nspredicate *pred = [nspredicate predicatewithformat:@"self matches %@", regex];
bool ismatch = [pred evaluatewithobject:txtfldphonenumber.text];
cocoa用nspredicate描述查询的方式,原理类似于在数据库中进行查询
用between,in,beginwith,endwith,contains,like这些谓词来构造nspredicate,必要的时候使用self直接对自己进行匹配
//基本的查询
nspredicate *predicate;
predicate = [nspredicate predicatewithformat: @"name == 'herbie'"];
bool match = [predicate evaluatewithobject: car];
nslog (@"%s", (match) ? "yes" : "no");
//在整个cars里面循环比较
predicate = [nspredicate predicatewithformat: @"engine.horsepower > 150"];
nsarray *cars = [garage cars];
for (car *car in [garage cars]) {
if ([predicate evaluatewithobject: car]) {
nslog (@"%@", car.name);
}
}
//输出完整的信息
nsarray *results;
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@", results);
//含有变量的谓词
nspredicate *predicatetemplate = [nspredicate predicatewithformat:@"name == $name"];
nsdictionary *vardict;
vardict = [nsdictionary dictionarywithobjectsandkeys:
@"herbie", @"name", nil];
predicate = [predicatetemplate predicatewithsubstitutionvariables: vardict];
nslog(@"snorgle: %@", predicate);
match = [predicate evaluatewithobject: car];
nslog (@"%s", (match) ? "yes" : "no");
//注意不能使用$variable作为路径名,因为它值代表值
predicate = [nspredicate predicatewithformat:
@"(engine.horsepower > 50) and (engine.horsepower < 200)"];
nslog (@"oop %@", results);
predicate = [nspredicate predicatewithformat: @"name < 'newton'"];
nslog (@"%@", [results valueforkey: @"name"]);
//强大的数组运算符
@"engine.horsepower between { 50, 200 }"];
nsarray *betweens = [nsarray arraywithobjects:
[nsnumber numberwithint: 50], [nsnumber numberwithint: 200], nil];
predicate = [nspredicate predicatewithformat: @"engine.horsepower between %@", betweens];
predicatetemplate = [nspredicate predicatewithformat: @"engine.horsepower between $powers"];
vardict = [nsdictionary dictionarywithobjectsandkeys: betweens, @"powers", nil];
//in运算符
predicate = [nspredicate predicatewithformat: @"name in { 'herbie', 'snugs', 'badger', 'flap' }"];
predicate = [nspredicate predicatewithformat: @"self.name in { 'herbie', 'snugs', 'badger', 'flap' }"];
names = [cars valueforkey: @"name"];
predicate = [nspredicate predicatewithformat: @"self in { 'herbie', 'snugs', 'badger', 'flap' }"];
results = [names filteredarrayusingpredicate: predicate];//这里限制了self的范围
//beginswith,endswith,contains
//附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
predicate = [nspredicate predicatewithformat: @"name beginswith 'bad'"];
predicate = [nspredicate predicatewithformat: @"name beginswith 'herb'"];
predicate = [nspredicate predicatewithformat: @"name beginswith[cd] 'herb'"];
//like运算符(通配符)
predicate = [nspredicate predicatewithformat: @"name like[cd] '*er*'"];
predicate = [nspredicate predicatewithformat: @"name like[cd] '???er*'"];
//基本的查询
nspredicate *predicate;
predicate = [nspredicate predicatewithformat: @"name == 'herbie'"];
bool match = [predicate evaluatewithobject: car];
nslog (@"%s", (match) ? "yes" : "no");
//在整个cars里面循环比较
predicate = [nspredicate predicatewithformat: @"engine.horsepower > 150"];
nsarray *cars = [garage cars];
for (car *car in [garage cars]) {
if ([predicate evaluatewithobject: car]) {
nslog (@"%@", car.name);
}
}
//输出完整的信息
nsarray *results;
results = [cars filteredarrayusingpredicate: predicate];
nslog (@"%@", results);
//含有变量的谓词
nspredicate *predicatetemplate = [nspredicate predicatewithformat:@"name == $name"];
nsdictionary *vardict;
vardict = [nsdictionary dictionarywithobjectsandkeys:
@"herbie", @"name", nil];
predicate = [predicatetemplate predicatewithsubstitutionvariables: vardict];
nslog(@"snorgle: %@", predicate);
match = [predicate evaluatewithobject: car];
nslog (@"%s", (match) ? "yes" : "no");
//注意不能使用$variable作为路径名,因为它值代表值
//谓词字符窜还支持c语言中一些常用的运算符
predicate = [nspredicate predicatewithformat:
@"(engine.horsepower > 50) and (engine.horsepower < 200)"];
nslog (@"oop %@", results);
predicate = [nspredicate predicatewithformat: @"name < 'newton'"];
nslog (@"%@", [results valueforkey: @"name"]);
//强大的数组运算符
@"engine.horsepower between { 50, 200 }"];
nsarray *betweens = [nsarray arraywithobjects:
[nsnumber numberwithint: 50], [nsnumber numberwithint: 200], nil];
predicate = [nspredicate predicatewithformat: @"engine.horsepower between %@", betweens];
predicatetemplate = [nspredicate predicatewithformat: @"engine.horsepower between $powers"];
vardict = [nsdictionary dictionarywithobjectsandkeys: betweens, @"powers", nil];
//in运算符
predicate = [nspredicate predicatewithformat: @"name in { 'herbie', 'snugs', 'badger', 'flap' }"];
predicate = [nspredicate predicatewithformat: @"self.name in { 'herbie', 'snugs', 'badger', 'flap' }"];
names = [cars valueforkey: @"name"];
predicate = [nspredicate predicatewithformat: @"self in { 'herbie', 'snugs', 'badger', 'flap' }"];
results = [names filteredarrayusingpredicate: predicate];//这里限制了self的范围
//beginswith,endswith,contains
//附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
predicate = [nspredicate predicatewithformat: @"name beginswith 'bad'"];
predicate = [nspredicate predicatewithformat: @"name beginswith 'herb'"];
predicate = [nspredicate predicatewithformat: @"name beginswith[cd] 'herb'"];
//like运算符(通配符)
predicate = [nspredicate predicatewithformat: @"name like[cd] '*er*'"];
predicate = [nspredicate predicatewithformat: @"name like[cd] '???er*'"];