Objective C USES regular expressions for matching in iOS App development
- 2020-06-03 08:29:12
- OfStack
There are three ways to achieve regular expression matching in iOS. Now record them all here:
1. Use NSPredicate (predicate) matching
For example, match 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 flexible, but requires knowledge of predicates.
2. Use rangeOfString: option: direct search
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]);
}
Setting NSRegularExpressionSearch in options means the position where the first result will be returned using a regular expression match.
3. Use regular expression classes
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 matches.
Example:
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 an Foundation class for queries, similar in principle and usage to where in SQL.
2. Verify your phone number
Simple 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 simply a matter of determining the phone number format. In fact, the format of mobile phones is a bit complicated.
Detailed judgment method
// Regex determines the phone number format
+ (BOOL)validatePhone:(NSString *)phone
{
/**
* Mobile phone number
* Mobile: 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* Unicom: 130,131,132,152,155,156,185,186
* Telecommunication: 133,1349,153,180,189
*/
NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
/**
* China Mobile: China Mobile
* 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
*/
NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
/**
* China 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: China Telecom
* 133,1349,153,180,189
*/
NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
/**
* Mainland area fixed telephone and PHS
* The area code: 010,020,021,022,023,024,025,027,028,029
* Number: 7 or 8 position
*/
// NSString * 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(@"China Mobile");
} else if([regextestct evaluateWithObject:phone] == YES) {
NSLog(@"China Telecom");
} else if ([regextestcu evaluateWithObject:phone] == YES) {
NSLog(@"China Unicom");
} else {
NSLog(@"Unknow");
}
return YES;
}
else
{
return NO;
}
}