-
iOS 中可以通過 NSPredicate 來處理正則表達式。相關資料如下:
NSPredicate 蘋果官方文檔:
http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/predicates.html
Predicate format strings:
http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html
ICU 正則表達式規則:
http://www.icu-project.org/userguide/regexp.html在 iOS 中,我們使用 NSPredicate 的字符串比較功能來進行正則表達式處理,其比較關鍵字爲:
MATCHES
下面,列舉一個匹配6-15個由字母/數字組成的字符串的正則表達式,來看看 NSPredicate 的具體使用:
- NSString * regex = @"(^[A-Za-z0-9]{6,15}$)";
- NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
- BOOL isMatch = [pred evaluateWithObject:@"123456ABCde"];
-
- //郵箱
- + (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];
- }
- //手機號碼驗證
- + (BOOL) validateMobile:(NSString *)mobile
- {
- //手機號以13, 15,18開頭,八個 \d 數字字符
- 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];
- }
- //車牌號驗證
- + (BOOL) validateCarNo:(NSString *)carNo
- {
- NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-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];
- }
- //車型
- + (BOOL) validateCarType:(NSString *)CarType
- {
- NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
- NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
- return [carTest evaluateWithObject:CarType];
- }
- //用戶名
- + (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;
- }
- //密碼
- + (BOOL) validatePassword:(NSString *)passWord
- {
- NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
- NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
- return [passWordPredicate evaluateWithObject:passWord];
- }
- //暱稱
- + (BOOL) validateNickname:(NSString *)nickname
- {
- NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
- NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
- return [passWordPredicate evaluateWithObject:nickname];
- }
- //身份證號
- + (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];
- }
其實iOS中有三種方式來實現正則表達式的匹配。現在將他們都記錄在這裏:
1.利用NSPredicate(謂詞)匹配
例如匹配有效郵箱:
NSString *email = @“[email protected]”;
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];
謂詞匹配比較靈活,但是需要有謂詞的相關知識。
2.利用rangeOfString:option:直接查找
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]);
}
options中設定NSRegularExpressionSearch就是表示利用正則表達式匹配,會返回第一個匹配結果的位置。
3.使用正則表達式類
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]);
}
使用系統的正則表達式類(NSRegularExpression)會返回匹配的多個結果。
小結:
第一種匹配需要學習NSPredicate的寫法,需要查閱蘋果相關技術文檔;如果只關心第一個匹配的結果,第二種匹配較爲簡潔;如果需要匹配多個結果,同時匹配多次,第三種方式效率會更高。