您好,登錄后才能下訂單哦!
在代碼開發過程中,我們經常需要用來校驗郵箱、手機號等等,這個時候就需要用到正則表達式。在iOS開發中,能用來做正則校驗的有兩個 NSPredicate 和 NSRegularExpression 。
NSPredicate 能用來簡單做正則校驗,但是它的問題是存在校驗不出來的情況。
//NSString+RegEx.h #import <Foundation/Foundation.h> @interface NSString (RegEx) #pragma mark - NSPredicate - (BOOL)mars_matchedByPrdicateToRegEx:(NSString *)regEx; @end //NSString+RegEx.m #import "NSString+RegEx.h" @implementation NSString (RegEx) #pragma mark - NSPredicate - (BOOL)mars_matchedByPrdicateToRegEx:(NSString *)regEx{ NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regEx]; return [predicate evaluateWithObject:self]; } @end
NSRegularExpression 相對于 NSPredicate 功能就強大的多了,這也是iOS正則校驗的正統路子。
//NSString+RegEx.h #import <Foundation/Foundation.h> @interface NSString (RegEx) #pragma mark - NSRegularExpression //校驗是否匹配 - (BOOL)mars_matchedToRegEx:(NSString *)regEx; //匹配到的第一個字符串 - (NSString *)mars_firstMatchToRegEx:(NSString *)regEx; //所有匹配的字符串 - (NSArray *)mars_matchesToRegEx:(NSString *)regEx; //替換匹配到的字符串 - (NSString *)mars_stringByReplaceMatchesToRegEx:(NSString *)regEx replaceString:(NSString *)replaceString; @end //NSString+RegEx.m #import "NSString+RegEx.h" @implementation NSString (RegEx) #pragma mark - NSRegualrExpression //校驗是否匹配 - (BOOL)mars_matchedToRegEx:(NSString *)regEx{ NSError *error; NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regEx options:NSRegularExpressionCaseInsensitive error:&error]; NSUInteger number = [regularExpression numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)]; return number != 0; } //匹配到的第一個字符串 - (NSString *)mars_firstMatchToRegEx:(NSString *)regEx{ NSError *error; NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regEx options:NSRegularExpressionCaseInsensitive error:&error]; NSTextCheckingResult *firstMatch = [regularExpression firstMatchInString:self options:0 range:NSMakeRange(0, self.length)]; if (firstMatch) { NSString *result = [self substringWithRange:firstMatch.range]; return result; } return nil; } //所有匹配的字符串 - (NSArray *)mars_matchesToRegEx:(NSString *)regEx{ NSError *error; NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regEx options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *matchArray = [regularExpression matchesInString:self options:0 range:NSMakeRange(0, self.length)]; NSMutableArray *array = [NSMutableArray array]; if (matchArray.count != 0) { for (NSTextCheckingResult *match in matchArray) { NSString *result = [self substringWithRange:match.range]; [array addObject:result]; } } return array; } //替換匹配到的字符串 - (NSString *)mars_stringByReplaceMatchesToRegEx:(NSString *)regEx replaceString:(NSString *)replaceString{ NSError *error; NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:regEx options:NSRegularExpressionCaseInsensitive error:&error]; return [regularExpression stringByReplacingMatchesInString:self options:0 range:NSMakeRange(0, self.length) withTemplate:replaceString]; } @end
最后我們看到,還是推薦大家使用NSRegularExpression來做正則的校驗,如果大家在學習中有更好的解決方法或者心得,可以在下方的留言區討論。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。