前段时间遇到后台返回数据,需要解析Html格式数据,经过折腾了一会就直接写了一个方法调用 其实很简单 我直接粘贴代码 下方再附上一张代码的截图 直接看图片的代码更容易一点
+(NSMutableArray*)AnalysisHtml:(NSString *)html{ NSMutableArray *htmlarr =[[NSMutableArray alloc]init]; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"]*/>" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil];
NSArray *result = [regex matchesInString:html options:NSMatchingReportCompletion range:NSMakeRange(0, html.length)];
for (NSTextCheckingResult *item in result) {
NSString *imgHtml = [html substringWithRange:[item rangeAtIndex:0]];
NSArray *tmpArray = nil;
if ([imgHtml rangeOfString:@"src=\""].location != NSNotFound) {
tmpArray = [imgHtml componentsSeparatedByString:@"src=\""];
} else if ([imgHtml rangeOfString:@"src="].location != NSNotFound) {
tmpArray = [imgHtml componentsSeparatedByString:@"src="];
}
if (tmpArray.count >= 2) {
NSString *src = tmpArray[1];
NSUInteger loc = [src rangeOfString:@"\""].location;
if (loc != NSNotFound) {
src = [src substringToIndex:loc];
NSLog(@"正确解析出来的SRC为:%@", src);
[htmlarr addObject:src];
}
}
}
return htmlarr;
}
代码链接:https://github.com/15626036029/Html
网友评论