繁体   English   中英

替换html字符串中的特定单词

[英]Replace specific words in html string

我有一个动态字符串,即currentString。 例如currentstring像:

<html><head><title></title><meta content="width=320.000000, initial-scale=0.47, maximum-scale=1.0, user-scalable=1" name="viewport"></head><body><table width="510" cellpadding="0" cellpadding="0"><tr><td valign="top"><p><a href="http://erhandemirci.blogspot.com/masak-in-baskani-neden-gorevden-alindi-haberi-828402.html"><img src="http://erhandemirci.blogspot.com/images//news/r-farukeliedioglu-300200-828402.jpg" width="72" height="48" style="border: 1px #000000 solid;" hspace="2" align="left"></a>content...........</p> <p> </p> </td></tr></table></body></html>

我想将表格标签的宽度从510更改为0。我尝试了以下代码,但无法正常工作。

NSString *currentString = @"<html><...width > <table width="" .... > dynamic string";

    // Regular expression to find "word characters" enclosed by {...}:
    NSRegularExpression *regex;
    regex = [NSRegularExpression regularExpressionWithPattern:@"\\table width=\"(\\w+)\\\""
                                                      options:0
                                                        error:NULL];

    NSMutableString *modifiedString = [currentString mutableCopy];
    __block int offset = 0;
    [regex enumerateMatchesInString:currentString
                            options:0
                              range:NSMakeRange(0, [currentString length])
                         usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                             // range = location of the regex capture group "(\\w+)" in currentString:
                             NSRange range = [result rangeAtIndex:1];
                             // Adjust location for modifiedString:
                             range.location += offset;

                             // Get old word:
                             NSString *oldWord = [modifiedString substringWithRange:range];

                             // Compute new word:
                             // In your case, that would be
                             // NSString *newWord = [self replaceWord:oldWord];
                             NSString *newWord =@"0";

                             // Replace new word in modifiedString:
                             [modifiedString replaceCharactersInRange:range withString:newWord];
                             // Update offset:
                             offset += [newWord length] - [oldWord length];
                         }
     ];


    NSLog(@"modified%@", modifiedString);

你猜对了差不多吧,只是@"\\\\table ...应该是@"\\\\<table ...的格局:

regex = [NSRegularExpression regularExpressionWithPattern:@"\\<table width=\"(\\w+)\\\""
                                                  options:0
                                                    error:NULL];

对于任意NNN这会将<table width="NNN"替换为<table width="0"

请注意,通常不建议使用正则表达式解析HTML。 使用专用的HTML解析器可能是更好的方法。

我不知道RegEx。 我将使用NSMutableString方法(例如replaceOccurrencesOfString:withString:options:range:解决此问题replaceOccurrencesOfString:withString:options:range:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM