美文网首页
NSRegularExpression

NSRegularExpression

作者: jay_丶 | 来源:发表于2020-03-18 14:22 被阅读0次
    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

          本文链接:https://www.haomeiwen.com/subject/pzgqyhtx.html