Use of Regular Expression in iOS -- NSPredicate

Source: Internet
Author: User

Here is an example:
Match the Regular Expression of 9-15 strings consisting 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 the query method. The principle is similar to querying in a database.

Use BETWEEN, IN, BEGINWITH, ENDWITH, CONTAINS, and LIKE predicates to construct NSPredicate. If necessary, use SELF to directly match yourself.

 

[Cpp]
// Basic query
NSPredicate * predicate;
Predicate = [NSPredicate predicateWithFormat: @ "name = 'herbie '"];
BOOL match = [predicate evaluateWithObject: car];
NSLog (@ "% s", (match )? "YES": "NO ");
// Compare loops in the whole cars
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower> 150"];
NSArray * cars = [garage cars];
For (Car * car in [garage cars]) {
If ([predicate evaluateWithObject: car]) {
NSLog (@ "% @", car. name );
}
}
// Output complete information
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower> 150"];
NSArray * results;
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// The predicate containing the variable
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 that $ VARIABLE cannot be used as the path name because its value represents the value.
// Supports common operators in C Language

Predicate = [NSPredicate predicateWithFormat:
@ "(Engine. horsepower> 50) 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]; // The SELF range is limited here.
NSLog (@ "% @", results );
// BEGINSWITH, ENDSWITH, CONTAINS
// Additional symbols, [c], [d], [cd], and c indicate case-insensitive, d indicates non-distinguishing pronunciation characters, and cd indicates no distinguishing between words.
Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH 'bad'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );

Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH 'hb'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );

Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH [cd] 'herb'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// LIKE operator (wildcard)
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 ");
// Compare loops in the whole cars
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower> 150"];
NSArray * cars = [garage cars];
For (Car * car in [garage cars]) {
If ([predicate evaluateWithObject: car]) {
NSLog (@ "% @", car. name );
}
}
// Output complete information
Predicate = [NSPredicate predicateWithFormat: @ "engine. horsepower> 150"];
NSArray * results;
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// The predicate containing the variable
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 that $ VARIABLE cannot be used as the path name because its value represents the value.
// Supports common operators in C Language

Predicate = [NSPredicate predicateWithFormat:
@ "(Engine. horsepower> 50) 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]; // The SELF range is limited here.
NSLog (@ "% @", results );
// BEGINSWITH, ENDSWITH, CONTAINS
// Additional symbols, [c], [d], [cd], and c indicate case-insensitive, d indicates non-distinguishing pronunciation characters, and cd indicates no distinguishing between words.
Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH 'bad'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );

Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH 'hb'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );

Predicate = [NSPredicate predicateWithFormat: @ "name BEGINSWITH [cd] 'herb'"];
Results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@ "% @", results );
// LIKE operator (wildcard)
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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.