In IOS, we use the string comparison feature of nspredicate for regular expression processing with the following keywords:MATCHES
Here's a regular expression that matches 6-15 letters/numbers, to see how nspredicate is used:
NSString * Regex = @ "(^[a-za-z0-9]{6,15}$)"; Nspredicate * pred = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex]; BOOL IsMatch = [pred evaluatewithobject:@ "123456ABCde"];
Here are some common regular expressions//mailbox + (BOOL) Validateemail: (NSString *) Email {nsstring *emailregex = @ "[a-z0-9a-z._%+-][email p Rotected][a-za-z0-9.-]+\\. [A-za-z] {2,4} "; Nspredicate *emailtest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Emailregex]; return [Emailtest Evaluatewithobject:email]; }//Mobile number verification + (BOOL) Validatemobile: (NSString *) Mobile {//Mobile phone number starts with 13, 15, 18, eight \d numeric characters nsstring *phoneregex = @ "^ ((13[0-9]) | (15[^4,\\d]) | (18[0,0-9])) \\d{8}$ "; Nspredicate *phonetest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Phoneregex]; return [Phonetest Evaluatewithobject:mobile]; }//Vehicle grade verification + (BOOL) Validatecarno: (NSString *) Carno {nsstring *carregex = @ "^[\u4e00-\u9fa5]{1}[a-za-z]{1}[a-z A-z_0-9]{4}[a-za-z_0-9_\u4e00-\u9fa5]$ "; Nspredicate *cartest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Carregex]; NSLog (@ "Cartest is%@", cartest); return [Cartest Evaluatewithobject:carno]; }//Model + (BOOL) validAtecartype: (NSString *) Cartype {nsstring *cartyperegex = @ "^[\u4e00-\u9fff]+$"; Nspredicate *cartest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Cartyperegex]; return [Cartest Evaluatewithobject:cartype]; }//user name + (BOOL) Validateusername: (NSString *) name {NSString *usernameregex = @ "^[a-za-z0-9]{6,20}+$"; Nspredicate *usernamepredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Usernameregex]; BOOL B = [Usernamepredicate evaluatewithobject:name]; return B; }//Password + (BOOL) ValidatePassword: (NSString *) PassWord {nsstring *passwordregex = @ "^[a-za-z0-9]{6,20}+$"; Nspredicate *passwordpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Passwordregex]; return [Passwordpredicate Evaluatewithobject:password]; }//Nickname + (BOOL) Validatenickname: (NSString *) Nickname {NSString *nicknameregex = @ "^[\u4e00-\u9fa5]{4,8}$"; Nspredicate *passwordpredicate = [Nspredicate Predicatewithformat:@ "Self MATCHES%@", Nicknameregex]; return [Passwordpredicate Evaluatewithobject:nickname]; }//Social Security number + (BOOL) Validateidentitycard: (NSString *) Identitycard {BOOL flag; if (identitycard.length <= 0) {flag = NO; return flag; } nsstring *regex2 = @ "^ (\\d{14}|\\d{17}) (\\d|[ XX]) $ "; Nspredicate *identitycardpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Regex2]; return [Identitycardpredicate Evaluatewithobject:identitycard]; }
In fact, there are three ways to match regular expressions in iOS. Now put them all on the record here:
1. Using nspredicate (predicate) matching
For example, to match a valid mailbox:
NSString *email = @ "[email protected]";
NSString *regex = @ "[A-z0-9a-z._%+-][email protected][a-za-z0-9.-]+\\. [A-za-z] {2,4} ";
Nspredicate *predicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];
BOOL isValid = [predicate evaluatewithobject:email];
predicate matching is more flexible, but requires knowledge of predicates.
2. Using rangeofstring:option: Direct lookup
NSString *searchtext = @ "//do any additional setup after loading the view, typically from a nib.";
Nsrange range = [SearchText rangeofstring:@ "(?: [^,]) *\\." Options:nsregularexpressionsearch ";
if (range.location! = nsnotfound) {
NSLog (@ "%@", [SearchText Substringwithrange:range]);
}
The set nsregularexpressionsearch in options means that a regular expression match is used to return the position of the first matching result.
3. Using the Regular expression class
NSString *searchtext = @ "//do any additional setup after loading the view, typically from a nib.";
Nserror *error = NULL;
Nsregularexpression *regex = [Nsregularexpression regularexpressionwithpattern:@ "(?: [^,]) *\\." Options: Nsregularexpressioncaseinsensitive error:&error];
Nstextcheckingresult *result = [Regex firstmatchinstring:searchtext options:0 range:nsmakerange (0, [searchText length] )];
if (result) {
NSLog (@ "%@\n", [SearchText SubstringWithRange:result.range]);
}
Using the system's regular expression class (Nsregularexpression) returns multiple results that match.
Summary:
The first match needs to learn Nspredicate's writing, need to consult Apple related technical documents; If you only care about the results of the first match, the second match is more concise; If you need to match multiple results and match them multiple times, the third way is more efficient.
Common Regular Expressions