zoukankan      html  css  js  c++  java
  • IOS 用正则表达式解析HTML等文件,得到所有文本

     获得网页内容

    NSURL *url=[NSURL URLWithString:@"http://121.199.34.52/wordpress/?json=core.get_post_content&post_id=8764&post_type=post"];
         NSDictionary * dic=[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:url] options:0 error:Nil];
     
      NSString *content=[dic objectForKey:@"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:@""];
        for (NSString *str in marr) {
               NSLog(@"呵呵-------------%@",str);
            
        }

    去除字符串中所有得空格及控制字符:

    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];

  • 相关阅读:
    新增html元素的使用
    音频的使用和插入以及动态文字的使用
    创建表单以及表单元素的使用
    今天学习image在html中的应用
    Repeater里查找控件
    CSS 基础教程
    自然图像的颜色统计特征 Image Color Statistics
    [转]UE中使用正则表达式的一些技巧
    [转]SQL Server 的事务和锁
    [转]Amtura 商务智能项目实现手记
  • 原文地址:https://www.cnblogs.com/huntaiji/p/3513172.html
Copyright © 2011-2022 走看看