NSString * htmlString = @"<a class='blue' href='http://10.5.225.14:8080/UserCenter/toUserIndex/ecfbdc8c98be410a9ad238511dc16e06.html' target='_blank'>@王琦琦 </a>jksjkdfsdfjsd<a class='blue' href='http://10.5.225.14:8080/UserCenter/toUserIndex/bb4294141cd141b0ba69d788811b7b7d.html' target='_blank'>@leef </a>jfsdlkfjsd<a class='blue' href='http://10.5.225.14:8080/UserCenter/toUserIndex/40c4973fe3fc4d178ffb123e2f765c79.html' target='_blank'>@lee </a>[色]";
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
NSLog(@"获取的文本是:%@",attrStr);
NSError *error;
NSString *regulaStr = @"\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *arrayOfAllMatches = [regex matchesInString:attrStr.string options:0 range:NSMakeRange(0, [htmlString length])];
NSLog(@"%@",arrayOfAllMatches);
for (NSTextCheckingResult *match in arrayOfAllMatches)
{
NSString* substringForMatch = [htmlString substringWithRange:match.range];
NSLog(@"%@",substringForMatch);
}
UILabel * myLabel = [[UILabel alloc] initWithFrame:self.view.bounds];
myLabel.attributedText = attrStr;
[self.view addSubview:myLabel];