正規表示式在iOS開發中的應用

Vince發表於2018-01-29

在程式碼開發過程中,我們經常需要用來校驗郵箱、手機號等等,這個時候就需要用到正規表示式。在iOS開發中,能用來做正則校驗的有兩個 NSPredicateNSRegularExpression

NSPredicate

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 (推薦)

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

相關文章