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

标签 ios iphone objective-c nsstring nsregularexpression

我有 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];

这将取代 <table width="NNN"通过 <table width="0"任意 NNN .

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

关于ios - 替换 html 字符串中的特定单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20729812/

相关文章:

iphone - pushViewController :animated: inside ScrollView

ios - 绘制bezierPath

ios - ImagePickerController 裁剪问题

ios - 如何强制 View 在 iOS 中进入横向?

iphone - Cocoa Touch : What to release, 什么时候发布?

ios - 在iOS中出现之前添加按钮渐变

ios - UItextfield 中的大写字符

objective-c - 如何使用quickdialog实现滑动删除

ios - 核心数据逻辑分布?

ios - addressDictionary 随机返回 nil