微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

特殊字符正则表达式查询

个人随笔记录:


- (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;
}

原文地址:https://www.jb51.cc/regex/360831.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐