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

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

    - (NSString *)changeToString:(NSString *)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:@""];
        NSMutableString *string = [[NSMutableString alloc] init];
        for (int i = 0; i < arr.count; i++) {
            [string appendString:[NSString stringWithFormat:@"%@",arr[i]]];
        }
        return  string;
    }
    
  • 相关阅读:
    Openlayer 3 的画图测量面积
    Openlayer 3 的画线测量长度
    屏幕尺寸
    px和em,rem的区别
    水平和垂直居中
    Flex布局
    继承的几种方法及优缺点
    call ,apply 和 bind的用法与区别
    mybatis springmvc velocity的demo
    正则同时包含两个关键字
  • 原文地址:https://www.cnblogs.com/iyou/p/4858655.html
Copyright © 2011-2022 走看看