在代碼開發(fā)過程中,我們經(jīng)常需要用來校驗(yàn)郵箱、手機(jī)號(hào)等等,這個(gè)時(shí)候就需要用到正則表達(dá)式。在iOS開發(fā)中,能用來做正則校驗(yàn)的有兩個(gè) NSPredicate 和 NSRegularExpression 。
NSPredicate 能用來簡單做正則校驗(yàn),但是它的問題是存在校驗(yàn)不出來的情況。
//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 相對(duì)于 NSPredicate 功能就強(qiáng)大的多了,這也是iOS正則校驗(yàn)的正統(tǒng)路子。
//NSString+RegEx.h#import <Foundation/Foundation.h>@interface NSString (RegEx)#pragma mark - NSRegularExpression//校驗(yàn)是否匹配- (BOOL)mars_matchedToRegEx:(NSString *)regEx;//匹配到的第一個(gè)字符串- (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//校驗(yàn)是否匹配- (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;}//匹配到的第一個(gè)字符串- (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來做正則的校驗(yàn),如果大家在學(xué)習(xí)中有更好的解決方法或者心得,可以在下方的留言區(qū)討論。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注