特殊字符正则表达式查询
发布时间:2020-12-14 01:27:07 所属栏目:百科 来源:网络整理
导读:个人随笔记录: - (NSMutableAttributedString *)filterLinkWithContent:(NSString *)content { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:content]; NSError *error = NULL; NSRegularExpression
个人随笔记录:
- (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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |