There are three ways to implement regular expression matching in iOS. Now record them all here:
1. Using nspredicate (predicate) matching
For example, to match a valid mailbox:
NSString *email = @ "nijino_saki@163.com";
nsstring *regex = @ "[a-z0-9a-z._%+-]+@[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 it requires knowledge of predicates.
2. Using rangeofstring:option: Direct lookup
NSString *searchtext = @ "//do no 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 options set Nsregularexpressionsearch means that a regular expression match is used to return the position of the first matching result.
3. Using Regular Expression Classes
NSString *searchtext = @ "//do no 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.
Instance:
1. Verify the Mailbox
+ (BOOL) Validateemail: (NSString *) email
{
NSString *emailregex = @ "[a-z0-9a-z._%+-]+@[a-za-z0-9.-]+\\.[ a-za-z]{2,4} ";
Nspredicate *emailtest = [nspredicate predicatewithformat:@ "SELF matches%@", Emailregex];
return [Emailtest Evaluatewithobject:email];
}
Nspredicate is a foundation class that is used for querying, and the principle and usage are similar to where in SQL.
2. Verify mobile phone number
Simple method of judgment
+ (BOOL) Validatephone: (NSString *) phone
{
nsstring *phoneregex = @ "1[3|5|7|8|] [0-9] {9} ";
Nspredicate *phonetest = [nspredicate predicatewithformat:@ "SELF matches%@", Phoneregex];
return [Phonetest Evaluatewithobject:phone];
}
It's just a simple way to judge the phone number format. In fact, the format of mobile phone is still a little complicated.
A detailed method of judgment
Regular Judge Mobile number format + (BOOL) Validatephone: (NSString *) Phone {/** * mobile number * Mobile: 134[0-8],135,136,137,138,139,150,151,1 57,158,159,182,187,188 * Unicom: 130,131,132,152,155,156,185,186 * Telecom: 133,1349,153,180,189/NSString * MOBILE
= @ "^1 (3[0-9]|5[0-35-9]|8[025-9]) \\d{8}$"; /** * Mobile: China Mobile * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188/N sstring * CM = @ "^1 (34[0-8]| (
3[5-9]|5[017-9]|8[278]) \\d) \\d{7}$ "; /** * Unicom: China Unicom * 130,131,132,152,155,156,185,186 * * NSString * CU = @ "^1 (3[0-2]|5[
256]|8[56]) \\d{8}$ "; /** * China Telecom: Chinese Telecom * 133,1349,153,180,189 * NSString * CT = @ "^1 ((33|53|8[09)) [0-9]|
349) \\d{7}$ "; /** * Mainland Region fixed and PHS * Area code: 010,020,021,022,023,024,025,027,028,029 * Number: seven-bit or eight-bit//NSST
Ring * PHS = @ "^0 (10|2[0-5789]|\\d{3}) \\d{7,8}$"; Nspredicate *regextestmobile = [nspredicate predicatewithformat:@ "SELF matches%@", MOBILE);
Nspredicate *REGEXTESTCM = [nspredicate predicatewithformat:@ "SELF matches%@", CM];
Nspredicate *REGEXTESTCU = [nspredicate predicatewithformat:@ "SELF matches%@", CU];
Nspredicate *REGEXTESTCT = [nspredicate predicatewithformat:@ "SELF matches%@", CT); if ([regextestmobile evaluatewithobject:phone] = = YES) | | ([regextestcm Evaluatewithobject:phone] = YES) | | ([regextestct Evaluatewithobject:phone] = YES) | |
([regextestcu Evaluatewithobject:phone] = YES)
{if ([regextestcm evaluatewithobject:phone] = YES) {NSLog (@ "Mobile");
else if ([regextestct evaluatewithobject:phone] = = YES) {NSLog (@ "Telecom");
else if ([regextestcu evaluatewithobject:phone] = = YES) {NSLog (@ "Unicom");
else {NSLog (@ "Unknow");
return YES;
else {return NO;
}
}