// 文本中是否与正则匹配
+ (BOOL) isMatchRule:(NSString *)rule Text:(NSString *)text
{
NSError *error = NULL;
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:rule options:0 error:&error];
NSUInteger *numCover = [regular numberOfMatchesInString:text options:0 range:NSMakeRange(0, text.length)];
regular = nil;
if (numCover)
{
return YES;
}else
{
return NO;
}
}
// 返回匹配的字符串形成的数组
+ (NSMutableArray *) getMatchRule:(NSString *)rule Text:(NSString *)text
{
NSMutableArray *matchStr = [[NSMutableArray alloc] init];
NSError *error = NULL;
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:rule options:0 error:&error];
NSArray *match = [regular matchesInString:text options:0 range:NSMakeRange(0, text.length)];
regular = nil;
for (NSTextCheckingResult *ma in match)
{
NSRange range = [ma range];
[matchStr addObject:[text substringWithRange:range]];
}
return matchStr;
}
// 将oldtext筛选出来的用newtext代替
+ (NSString *) replaceRule:(NSString *)rule OldText:(NSString *)oldtext NewText:(NSString *)newtext
{
NSError *error = NULL;
NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:rule options:0 error:&error];
NSString *replacedStr = [regular stringByReplacingMatchesInString:oldtext options:0 range:NSMakeRange(0, oldtext.length) withTemplate:newtext];
regular = nil;
return replacedStr;
}
- option 为block相应的选项,这里暂且不做描述