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;
    }
     
     
     
  • 相关阅读:
    【转】mxGraph教程-开发入门指南
    利用IPC通道进行进程间通信(C#)
    C++引用指针 & 构造函数
    MySQL配置主主及主从备份
    MySQL 主从热备份(读写分离)
    SqlServer双机热备技术实践笔记
    c#中的弱引用:WeakReference
    px、em、rem、%、vw、vh、vm这些单位的区别
    深浅clone
    JavaScript-原始值和引用值
  • 原文地址:https://www.cnblogs.com/liuyingjie/p/5286797.html
Copyright © 2011-2022 走看看