iOS中使用正则表达式去掉HTML中的标签元素获得纯文本的方法

content是根据网址获得的网页源码字符串

- (NSString *)changeToString:(NSString *)content
{
NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n"
options:0
error:nil]; content = [regularExpretion stringByReplacingMatchesInString:content options:NSMatchingReportProgress range:NSMakeRange(0, content.length) withTemplate:@"-"]; // 替换所有html和换行匹配元素为"-" regularExpretion = [NSRegularExpression regularExpressionWithPattern:@"-{1,}" options:0 error:nil] ;
content = [regularExpretion stringByReplacingMatchesInString:content options:NSMatchingReportProgress range:NSMakeRange(0, content.length) withTemplate:@"-"]; // 把多个"-"匹配为一个"-" // 根据"-"分割到数组
NSArray *arr=[NSArray array];
content = [NSString stringWithString:content];
arr = [content componentsSeparatedByString:@"-"];
NSMutableArray *marr=[NSMutableArray arrayWithArray:arr];
[marr removeObject:@""];
NSMutableString *string = [[NSMutableString alloc] init];
for (int i = 0; i < arr.count; i++) {
[string appendString:[NSString stringWithFormat:@"%@",arr[i]]];
}
return string;
}
上一篇:poj 1274 The Perfect Stall(二分图匹配)


下一篇:Python脚本文件(.py)打包为可执行文件(.exe)即避免命令行中包含Python解释器