特殊字符正则表达式查询

来源:互联网 发布:mac 命令行安装mysql 编辑:程序博客网 时间:2024/05/16 15:11

个人随笔记录:


- (NSMutableAttributedString *)filterLinkWithContent:(NSString *)content {    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content];    NSError *error = NULL;    NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"https://\\w+" options:0 error:nil];    NSDataDetector *detector =    [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber | NSTextCheckingTypeDate                                    error:&error];    NSArray *matches = [regularExpression matchesInString:content                                         options:0                                           range:NSMakeRange(0, [content length])];    for (NSTextCheckingResult *match in matches) {        NSLog(@"----%@", match);        if (match.range.location != NSNotFound)        {            [attributedString addAttribute:NSLinkAttributeName value:[content substringWithRange:match.range] range:match.range];        }        if (([match resultType] == NSTextCheckingTypeDate)) {                        NSDate *url = [match date];            [attributedString addAttribute:NSLinkAttributeName value:url range:match.range];        }    }    return attributedString;}


0 0