+ (nullable NSRegularExpression *)regularExpressionWithPattern:(NSString *)pattern options:(NSRegularExpressionOptions)options error:(NSError **)error; - (nullable instancetype)initWithPattern:(NSString *)pattern options:(NSRegularExpressionOptions)options error:(NSError **)error
該類中的屬性正則表達式
pattern
返回正則表達式模式options
返回建立正則表達式選項時使用的選項numberOfCaptureGroups
返回正則表達式模式options 定義的枚舉類型以下:express
typedef NS_OPTIONS(NSUInteger, NSRegularExpressionOptions) { NSRegularExpressionCaseInsensitive = 1 << 0, //不區分大小寫的 NSRegularExpressionAllowCommentsAndWhitespace = 1 << 1, //忽略空格和# - NSRegularExpressionIgnoreMetacharacters = 1 << 2, //總體化 NSRegularExpressionDotMatchesLineSeparators = 1 << 3, //匹配任何字符,包括行分隔符 NSRegularExpressionAnchorsMatchLines = 1 << 4, //容許^和$在匹配的開始和結束行 NSRegularExpressionUseUnixLineSeparators = 1 << 5, //(查找範圍爲整個無效) NSRegularExpressionUseUnicodeWordBoundaries = 1 << 6 //(查找範圍爲整個無效) };
//枚舉容許Block處理每一個正則表達式匹配的字符串 - (void)enumerateMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range usingBlock:(void (NS_NOESCAPE ^)(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL *stop))block; //返回一個數組,包含字符串中正則表達式的全部匹配項 - (NSArray<NSTextCheckingResult *> *)matchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range; //返回字符串指定範圍內匹配數 - (NSUInteger)numberOfMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range; //返回字符串指定範圍內第一個匹配項。 - (nullable NSTextCheckingResult *)firstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range; //返回字符串指定範圍內第一個匹配的範圍 - (NSRange)rangeOfFirstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
NSMatchingOptions的定義以下:segmentfault
typedef NS_OPTIONS(NSUInteger, NSMatchingOptions) { NSMatchingReportProgress = 1 << 0, /* 在長時間運行的匹配操做中按期調用Block */ NSMatchingReportCompletion = 1 << 1, /* 完成任何匹配後,調用Block一次*/ NSMatchingAnchored = 1 << 2, /*指定匹配僅限於搜索範圍開始時的匹配 */ NSMatchingWithTransparentBounds = 1 << 3, /* 定匹配能夠檢查超出搜索範圍的範圍的字符串的部分,以用於諸如字邊界檢測,前瞻等。若是搜索範圍包含整個字符串,該常量將不起做用 */ NSMatchingWithoutAnchoringBounds = 1 << 4 /* 指定^而且$不會自動匹配搜索範圍的開始和結束,但仍將與整個字符串的開頭和結尾相匹配。若是搜索範圍包含整個字符串,則該常量不起做用 */ };
//返回與模板字符串替換的匹配正則表達式的新字符串 - (NSString *)stringByReplacingMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range withTemplate:(NSString *)templ; //返回替換的個數 - (NSUInteger)replaceMatchesInString:(NSMutableString *)string options:(NSMatchingOptions)options range:(NSRange)range withTemplate:(NSString *)templ; //自定義替換功能 - (NSString *)replacementStringForResult:(NSTextCheckingResult *)result inString:(NSString *)string offset:(NSInteger)offset template:(NSString *)templ; //經過根據須要添加反斜槓轉義來返回模板字符串,以保護符合模式元字符的任何字符 + (NSString *)escapedTemplateForString:(NSString *)string;
使用示例api
NSString *str = @"aabbcccdeaargdo14141214aaghfh56821d3gad4"; NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"aa" options:NSRegularExpressionCaseInsensitive error:NULL]; if (expression != nil) { //匹配到的第一組 NSTextCheckingResult *firstMatch = [expression firstMatchInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)]; NSRange range = [firstMatch rangeAtIndex:0]; NSString *result = [str substringWithRange:range]; NSLog(@"匹配到的第一組:%@",result); //匹配到的個數 NSInteger number = [expression numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)]; NSLog(@"匹配到的個數%ld",number); //配到到的全部數據 NSArray *allMatch = [expression matchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)]; for (int i = 0; i < allMatch.count; i ++) { NSTextCheckingResult *matchItem = allMatch[i]; NSRange range = [matchItem rangeAtIndex:0]; NSString *result = [str substringWithRange:range]; NSLog(@"匹配到的數據:%@",result); } //匹配到第一組的位置 NSRange firstRange = [expression rangeOfFirstMatchInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)]; NSLog(@"匹配到第一組的位置:開始位置%lu--長度%lu",(unsigned long)firstRange.location,(unsigned long)firstRange.length); //替換字符串 NSString *resultStr = [expression stringByReplacingMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length) withTemplate:@"bbbb"]; NSLog(@"替換後的字符串:%@",resultStr); NSInteger resultNum = [expression replaceMatchesInString:[str mutableCopy] options:NSMatchingReportProgress range:NSMakeRange(0, str.length) withTemplate:@"bbbb"]; NSLog(@"替換的個數;%ld",(long)resultNum); } 打印log: 2017-08-13 23:28:53.898 NSRegularExpressionDemo[82046:8220904] 匹配到的第一組:aa NSRegularExpressionDemo[82046:8220904] 匹配到的個數3 NSRegularExpressionDemo[82046:8220904] 匹配到的數據:aa NSRegularExpressionDemo[82046:8220904] 匹配到的數據:aa NSRegularExpressionDemo[82046:8220904] 匹配到的數據:aa NSRegularExpressionDemo[82046:8220904] 匹配到第一組的位置:開始位置0--長度2 NSRegularExpressionDemo[82046:8220904] 替換後的字符串:bbbbbbcccdebbbbrgdo14141214bbbbghfh56821d3gad4 NSRegularExpressionDemo[82046:8220904] 替換的個數;3
//NSStringCompareOptions --> NSRegularExpressionSearch - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask; - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch; - (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch locale:(nullable NSLocale *)locale
從上面的api能夠看出,只能匹配到第一組數組
使用示例lua
NSString *str = @"aabbcccdeaargdo14141214aaghfh56821d3gad4"; NSRange strMatchStr = [str rangeOfString:@"aa" options:NSRegularExpressionSearch]; NSLog(@"匹配到字符串的位置:開始位置%lu--長度%lu",(unsigned long)strMatchStr.location,(unsigned long)strMatchStr.length) 打印log: NSRegularExpressionDemo[82080:8224265] 匹配到字符串的位置:開始位置0--長度2
使用示例spa
NSString *str2 = @"aabbcc"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^aa(.)*cc$"]; BOOL isMatch = [predicate evaluateWithObject:str2]; NSLog(@"匹配的結果:%d",isMatch); 打印log: NSRegularExpressionDemo[82679:8253019] 匹配的結果:1
正則語法code