Regular expressions in iOS [go]

Source: Internet
Author: User

1. One example of the following simple use of regular expressions: Nsregularexpression class

-(void) parsestring{

To assemble a string, you need to parse the URL inside it.

NSString *[email protected] "sfdsfhttp://www.baidu.com";

The method that calls the expression inside the Nsregularexpression class needs to pass a nserror argument. The following defines a

Nserror *error;

http+:[^\\s]* This expression is to detect a URL.

Nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:@ "http+:[^\\s]*" options:0 Error: &error];

if (regex! = nil) {

Nstextcheckingresult *firstmatch=[regex firstmatchinstring:urlstringoptions:0range:nsmakerange (0, [urlString Length]);

if (Firstmatch) {

Nsrange ResultRange = [Firstmatch rangeatindex:0]; Equivalent to the range of Firstmatch.range---matches

Intercept data from URLString

NSString *result=[urlstring Substringwithrange:resultrange];

Output results

NSLog (@ "%@", result);

}

}

}

2. Use regular expressions to determine

Initializes a Nsregularexpression object and sets the range of Detection objects to: 0-9

Nsregularexpression *regex2 = [nsregularexpression regularexpressionwithpattern:@ "^[0-9]*$" options:0 Error:nil];

if (REGEX2)

{//object to match

Nstextcheckingresult *RESULT2 = [Regex2 firstMatchInString:textField.text options:0 range:nsmakerange (0, [ Textfield.text length]);

if (RESULT2) {

}

}

1. Code that determines whether the mailbox is formatted correctly: Nspredicatel class

Using regular expression validation

Nspredicatel class: A condition that is used primarily to specify a filter, which can accurately describe the required conditions, filter each object through predicates, and determine if the condition matches. A predicate is a function in a computer that represents the calculation of a true or false value. Both the principle and the usage are similar to where in the SQL query, which acts as a filter for the database. Used primarily for sorting out eligible objects from a collection, or for regular matching of strings

-(BOOL) Isvalidateemail: (NSString *) email

{

NSString *emailregex = @ "[A-z0-9a-z._%+-][email protected][a-za-z0-9.-]+\\. [A-za-z] {2,4} ";

Nspredicate *emailtest = [Nspredicate predicatewithformat:@ "Self matches%@", Emailregex];

return [Emailtest Evaluatewithobject:email];

}

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

Regular expressions in iOS [go]

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.