300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > IOS使用正则表达式去掉html中的标签元素 获得纯文本

IOS使用正则表达式去掉html中的标签元素 获得纯文本

时间:2020-08-21 20:03:33

相关推荐

IOS使用正则表达式去掉html中的标签元素 获得纯文本

IOS使用正则表达式去掉html中的标签元素,获得纯文本

content是根据网址获得的网页源码字符串NSRegularExpression*regularExpretion=[NSRegularExpressionregularExpressionWithPattern:@"<[^>]*>|\n"options:0error:nil];content=[regularExpretionstringByReplacingMatchesInString:contentoptions:NSMatchingReportProgressrange:NSMakeRange(0, content.length)withTemplate:@"-"];//替换所有html和换行匹配元素为"-"regularExpretion=[NSRegularExpressionregularExpressionWithPattern:@"-{1,}"options:0error:nil] ;content=[regularExpretionstringByReplacingMatchesInString:contentoptions:NSMatchingReportProgressrange:NSMakeRange(0, content.length)withTemplate:@"-"];//把多个"-"匹配为一个"-"//根据"-"分割到数组NSArray*arr=[NSArrayarray];content=[NSStringstringWithString:content];arr = [contentcomponentsSeparatedByString:@"-"];NSMutableArray*marr=[NSMutableArrayarrayWithArray:arr];[marrremoveObject:@""];returnmarr;

//正则去除网络标签-(NSString *)getZZwithString:(NSString *)string{NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n"options:0error:nil];string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];return string;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。