2. Match 9-15 regular expressions with a string of letters/numbers:
NSString * regex = @ "^[a-za-z0-9]{9,15}$";
Nspredicate *pred = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];
BOOL IsMatch = [pred evaluateWithObject:txtfldPhoneNumber.text];
Cocoa uses nspredicate to describe a query in a way that is similar to querying in a database
Use the between,in,beginwith,endwith,contains,like to construct the nspredicate, and when necessary, match yourself directly with self
[CPP]
Basic Query
Nspredicate *predicate;
predicate = [Nspredicate predicatewithformat: @ "name = = ' Herbie '"];
BOOL match = [predicate evaluatewithobject:car];
NSLog (@ "%s", (match)? "YES": "NO");
In the whole of cars cycle comparison
predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower > 150"];
Nsarray *cars = [garage cars];
For (Car *car in [garage cars]) {
if ([predicate evaluatewithobject:car]) {
NSLog (@ "%@", car.name);
}
}
Output the complete information
predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower > 150"];
Nsarray *results;
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
predicates that contain variables
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");
Note You cannot use $variable as the pathname because its value represents the value
predicate character channeling also supports some common operators in C language
predicate = [Nspredicate Predicatewithformat:
@ "(Engine.horsepower >) and (Engine.horsepower < 200)"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "OOP%@", results);
predicate = [Nspredicate predicatewithformat: @ "Name < ' Newton '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", [Results Valueforkey: @ "name"]);
Powerful array operators
predicate = [Nspredicate Predicatewithformat:
@ "Engine.horsepower between {50, 200}"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
Nsarray *betweens = [Nsarray arraywithobjects:
[NSNumber numberwithint:50], [NSNumber numberwithint:200], nil];
predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower between%@", Betweens];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
Predicatetemplate = [Nspredicate predicatewithformat: @ "engine.horsepower between $POWERS"];
Vardict = [Nsdictionary dictionarywithobjectsandkeys:betweens, @ "POWERS", nil];
predicate = [Predicatetemplate predicatewithsubstitutionvariables:vardict];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
In operator
predicate = [Nspredicate predicatewithformat: @ "name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", [Results Valueforkey: @ "name"]);
predicate = [Nspredicate Predicatewithformat: @ "self.name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", [Results Valueforkey: @ "name"]);
names = [Cars Valueforkey: @ "name"];
predicate = [Nspredicate Predicatewithformat: @ "Self in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"];
results = [names filteredarrayusingpredicate:predicate];//This limits the scope of self
NSLog (@ "%@", results);
Beginswith,endswith,contains
additional symbols, [c],[d],[cd],c for case insensitive, d for non-distinguishing pronunciation characters, and CD for no distinction
predicate = [Nspredicate predicatewithformat: @ "name Beginswith ' bad '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
predicate = [Nspredicate predicatewithformat: @ "name Beginswith ' HERB '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
predicate = [Nspredicate predicatewithformat: @ "name BEGINSWITH[CD] ' HERB '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
Like operator (wildcard character)
predicate = [Nspredicate predicatewithformat: @ "name LIKE[CD] ' *er* '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
predicate = [Nspredicate predicatewithformat: @ "name LIKE[CD] '??? Er* ' "];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
Basic Query
Nspredicate *predicate;
predicate = [Nspredicate predicatewithformat: @ "name = = ' Herbie '"];
BOOL match = [predicate evaluatewithobject:car];
NSLog (@ "%s", (match)? "YES": "NO");
In the whole of cars cycle comparison
predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower > 150"];
Nsarray *cars = [garage cars];
For (Car *car in [garage cars]) {
if ([predicate evaluatewithobject:car]) {
NSLog (@ "%@", car.name);
}
}
Output the complete information
predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower > 150"];
Nsarray *results;
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
predicates that contain variables
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");
Note You cannot use $variable as the pathname because its value represents the value
predicate character channeling also supports some common operators in C language
predicate = [Nspredicate Predicatewithformat:
@ "(Engine.horsepower >) and (Engine.horsepower < 200)"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "OOP%@", results);
predicate = [Nspredicate predicatewithformat: @ "Name < ' Newton '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", [Results Valueforkey: @ "name"]);
Powerful array operators
predicate = [Nspredicate Predicatewithformat:
@ "Engine.horsepower between {50, 200}"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
Nsarray *betweens = [Nsarray arraywithobjects:
[NSNumber numberwithint:50], [NSNumber numberwithint:200], nil];
predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower between%@", Betweens];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
Predicatetemplate = [Nspredicate predicatewithformat: @ "engine.horsepower between $POWERS"];
Vardict = [Nsdictionary dictionarywithobjectsandkeys:betweens, @ "POWERS", nil];
predicate = [Predicatetemplate predicatewithsubstitutionvariables:vardict];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
In operator
predicate = [Nspredicate predicatewithformat: @ "name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", [Results Valueforkey: @ "name"]);
predicate = [Nspredicate Predicatewithformat: @ "self.name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", [Results Valueforkey: @ "name"]);
names = [Cars Valueforkey: @ "name"];
predicate = [Nspredicate Predicatewithformat: @ "Self in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"];
results = [names filteredarrayusingpredicate:predicate];//This limits the scope of self
NSLog (@ "%@", results);
Beginswith,endswith,contains
additional symbols, [c],[d],[cd],c for case insensitive, d for non-distinguishing pronunciation characters, and CD for no distinction
predicate = [Nspredicate predicatewithformat: @ "name Beginswith ' bad '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
predicate = [Nspredicate predicatewithformat: @ "name Beginswith ' HERB '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
predicate = [Nspredicate predicatewithformat: @ "name BEGINSWITH[CD] ' HERB '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
Like operator (wildcard character)
predicate = [Nspredicate predicatewithformat: @ "name LIKE[CD] ' *er* '"];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
predicate = [Nspredicate predicatewithformat: @ "name LIKE[CD] '??? Er* ' "];
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);