NSRegularExpression

NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"(?:\\n)+"
                                                                          options:NSRegularExpressionCaseInsensitive
                                                                                 |NSRegularExpressionDotMatchesLineSeparators
                                                                            error:nil];
        [regex enumerateMatchesInString:self.string
                                options:0
                                  range:NSMakeRange(0, self.string.length)
                             usingBlock:^(NSTextCheckingResult *result,
                                          NSMatchingFlags flags,
                                          BOOL *stop) {
            NSRange resRange = result.range;
            NSString *string = [self.string substringWithRange:resRange];
            NSRange range = [att.string rangeOfString:string];
            [att replaceCharactersInRange:range withString:@" "];
        }];

此方法为NSRegularExpression通过正则的最基本的匹配方法,其遍历string的每个match,并对每个match进行block中指定的操作,且可在任何执行步骤停止

你可能感兴趣的:(NSRegularExpression)