zoukankan      html  css  js  c++  java
  • IOS使用正则表达式去掉html中的标签元素,获得纯文本

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

     
    content是根据网址获得的网页源码字符串
    NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>| "
                                                                                        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:@""];
        return  marr;
     
     
    //正则去除网络标签
    -(NSString *)getZZwithString:(NSString *)string{
        NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|
    "
                                                                                        options:0
                                                                                          error:nil];
        string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
        return string;
    }
     
     
     
  • 相关阅读:
    2117 poj 割点练习
    hdu 2767强连通分量练习
    hdu 1301 kruskal 最小生成树
    hdu 1523 求割点和块
    hdu 1207Arbitrage 最短路劲
    hdu 1874 畅通工程续
    求最小点基 poj 1236
    Hdu 1301 prim算法 生成最小生成树
    我眼中的性能测试工程师
    Web系统的测试
  • 原文地址:https://www.cnblogs.com/liuyingjie/p/5286797.html
Copyright © 2011-2022 走看看