ios 使用正则表达式

通过NSRegularExpression进行匹配字符串。

正则写的不好,分开几条。

首先替换修改table的width.

接着修改td的高度

最后在td数据上增加 font

在手机上看起来会更美观点。


比较详细点的可以查看 点击打开链接

直接发代码好了

    sHTML = [sHTML stringByReplacingOccurrencesOfString:@"\n\t" withString:@""];
    NSError *error = nil;
    //width
    NSString *sReg = @"width: (\\d+)px;";
    NSRegularExpression * reg = [NSRegularExpression regularExpressionWithPattern:sReg
                                                                          options:NSRegularExpressionCaseInsensitive
                                                                            error:&error];
    
    if (error == nil && reg)
    {
        NSArray *match = [reg matchesInString:sHTML
                                      options:NSMatchingReportProgress
                                        range:NSMakeRange(0, sHTML.length)];
        if ([match count] > 0)
        {
            sHTML = [reg stringByReplacingMatchesInString:sHTML
                                                  options:NSMatchingReportProgress
                                                    range:NSMakeRange(0, sHTML.length)
                                             withTemplate:@"width: 640px;"];
        }
    }
    
    //height
    NSString *stdHeight = @"height: (\\d+)px; ";
    NSRegularExpression *regHeight = [NSRegularExpression regularExpressionWithPattern:stdHeight
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:&error];
    if ( error == nil && regHeight)
    {
        sHTML = [regHeight stringByReplacingMatchesInString:sHTML
                                                    options:NSMatchingReportProgress
                                                      range:NSMakeRange(0, sHTML.length)
                                               withTemplate:@"height: 45px;"];
    }
    
    //add font
    NSString *sTDReg = @"<td.*>(.*)</td>";
    NSRegularExpression *replaceReg = [NSRegularExpression regularExpressionWithPattern:sTDReg
                                                                                options:NSRegularExpressionCaseInsensitive
                                                                                  error:&error];
    
    NSMutableString *sNewHTML = [NSMutableString stringWithString:sHTML];
    if (error == nil && replaceReg )
    {
        NSArray *match = [replaceReg matchesInString:sHTML
                                             options:NSMatchingReportProgress
                                               range:NSMakeRange(0, sHTML.length)];
        if ([match count] > 0)
        {
            NSInteger nIndex = 0;
            for (NSTextCheckingResult *result in match)
            {   
                NSString *sString = [sHTML substringWithRange:[result rangeAtIndex:1]];
                NSString *sReplaceString = [NSString stringWithFormat:@"<font size=\"6\">%@</font>", sString];
                NSRange range = [result rangeAtIndex:1];
                [sNewHTML replaceCharactersInRange:NSMakeRange(range.location + nIndex, range.length)
                                        withString:sReplaceString];
                nIndex += sReplaceString.length - sString.length;
            }
        }
    }
    
    [sNewHTML replaceOccurrencesOfString:@"\t"
                              withString:@""
                                 options:NSCaseInsensitiveSearch
                                   range:NSMakeRange(0, sNewHTML.length)];
    CC(@"replace html : %@", sNewHTML);
    
    return sNewHTML;


你可能感兴趣的:(ios 使用正则表达式)