一、介紹
在開發中,有時我們需要對一串字符串做特殊的處理后再使用,例如判斷是不是特殊字符、去掉所有的特殊字符等。做處理的方法有很多,最簡單的就是for循環遍歷一個個的比較處理,最好用的應該是使用正則表達式。
二、正則
三、API
NSRegularExpression匹配:
/* 遍歷的模式,正則表達式匹配在指定options和range模式下匹配指定string,傳入block中可以獲取結果信息 */ - (void)enumerateMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range usingBlock:(void (NS_NOESCAPE ^)(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL *stop))block; /* 在指定options和range模式下匹配指定string,通過正則匹配返回一個匹配結果的數組 */ - (NSArray<NSTextCheckingResult *> *)matchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range; /* 返回滿足條件的匹配次數 */ - (NSUInteger)numberOfMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range; /* 匹配返回的第一個結果,NSTextCheckingResult類型 */ - (nullable NSTextCheckingResult *)firstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range; /* 匹配返回的第一個結果的NSRange范圍信息 */ - (NSRange)rangeOfFirstMatchInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range;
相關的NSMatchingOptions 枚舉和NSMatchingFlags 枚舉
typedef NS_OPTIONS(NSUInteger, NSMatchingOptions) { NSMatchingReportProgress = 1 << 0, //找到最長的匹配字符串后調用block回調 NSMatchingReportCompletion = 1 << 1, //找到任何一個匹配串后都回調一次block NSMatchingAnchored = 1 << 2, //從匹配范圍的開始處進行匹配 NSMatchingWithTransparentBounds = 1 << 3, //允許匹配的范圍超出設置的范圍 NSMatchingWithoutAnchoringBounds = 1 << 4 //禁止^和$自動匹配行還是和結束 }; typedef NS_OPTIONS(NSUInteger, NSMatchingFlags) { NSMatchingProgress = 1 << 0, //匹配到最長串后被設置 NSMatchingCompleted = 1 << 1, //全部分配完成后被設置 NSMatchingHitEnd = 1 << 2, //匹配到設置范圍的末尾時被設置 NSMatchingRequiredEnd = 1 << 3, //當前匹配到的字符串在匹配范圍的末尾時被設置 NSMatchingInternalError = 1 << 4 //由於錯誤導致的匹配失敗時被設置 };
NSRegularExpression替換:
/* 在指定的options和指定的range中,用新字段替換原文本中的對應字段,並返回操作后的NSString */ - (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; /* 在 string 中查找由 result + offset 指定的字符串, 返回 template 指定的字符串(比如$0-9等) */ - (NSString *)replacementStringForResult:(NSTextCheckingResult *)result inString:(NSString *)string offset:(NSInteger)offset template:(NSString *)templ; /* 正則表達式字符串, 包括一些特殊字符. */ + (NSString *)escapedTemplateForString:(NSString *)string;
三、使用
(1)去掉所有的特殊字符和標點符號
+(NSString *)deleteCharacters:(NSString *)targetString{ if (targetString.length==0 || !targetString) { return nil; } NSError *error = nil; NSString *pattern = @"[^a-zA-Z0-9\u4e00-\u9fa5]";//正則取反 NSRegularExpression *regularExpress = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];//這個正則可以去掉所有特殊字符和標點 NSString *string = [regularExpress stringByReplacingMatchesInString:targetString options:0 range:NSMakeRange(0, [targetString length]) withTemplate:@""]; return string; }
(2)判斷是否是特殊字符或者標點符號
+(BOOL)isCharacters:(NSString *)targetString{ NSString *regex = @"[\u4e00-\u9fa5|0-9|a-zA-Z]"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex]; return ![pred evaluateWithObject:targetString]; }