Html 如何为Objective-C编写正则表达式?

Html 如何为Objective-C编写正则表达式?,html,objective-c,ios,regex,cocoa,Html,Objective C,Ios,Regex,Cocoa,我有一些html数据,其中包含一些img标记,如下所示: img width=500 height=400 img width=400 height=250 img width=600 height=470 高度和宽度总是在变化。我必须替换html数据。我需要使用Objective-C将该html数据替换为“img with=100” 我写了这些,但不匹配 NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpress

我有一些html数据,其中包含一些img标记,如下所示:

img width=500 height=400
img width=400 height=250
img width=600 height=470
高度和宽度总是在变化。我必须替换html数据。我需要使用Objective-C将该html数据替换为“img with=100”

我写了这些,但不匹配

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/(img\\s)((width|height)(=)([0-9]+)"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:myhtmldata
                                                    options:0
                                                      range:NSMakeRange(0, [myhtmldata length])];

NSString *modifiedString; 
if (numberOfMatches > 0)
{
   modifiedString = [regex stringByReplacingMatchesInString:myhtmldata
                                                           options:0
                                                             range:NSMakeRange(0, [myhtmldata length])
                                                      withTemplate:@"img width=30"];

}

您能帮助我吗?

如果我从示例代码中正确推断出意图,您只需使用
NSRegularExpression
将宽度更改为30。然后:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSError *regexError = nil;
        NSRegularExpressionOptions options = 0;
        NSString *sampleText = @"img width=500 height=400";
        NSString *pattern = @"^(img\\s+)width=\\d+(\\s+height=\\d+)";
        NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern options:options error:&regexError];

        sampleText = [expression stringByReplacingMatchesInString:sampleText
                                                          options:0
                                                            range:NSMakeRange(0,sampleText.length)
                                                     withTemplate:@"$1width=30$2"];
        printf("%s\n",[sampleText UTF8String]);
    }

}
然后将模板字符串更改为
@“$130”
。如果您对my original code进行了这些更改,那么您应该匹配嵌入HTML中的
img
标记的所有匹配项。例如,它应该改变:

<html>
    <body>
        <img width=500 height=400>
        <img width=520 height=100>
    </body>
</html>

致:



这就是您的规格要求吗?

我找到了一种不同的方法,它正在工作。以下是代码:

NSArray* ary = [oldHtml componentsSeparatedByString:@"<img"];
NSString* newHtml = [ary objectAtIndex:0];
for (int i = 1; i < [ary count]; i++) {

    newHtml = [newHtml stringByAppendingString:[@"<img width=300 " stringByAppendingString:[[ary objectAtIndex:i] substringFromIndex:[[ary objectAtIndex:i] rangeOfString:@"src"].location]]];

}

NSArray*ary=[oldHtml componentsSeparatedByString:@“您真的应该用正则表达式解析HTML吗?我只需要更改图像大小。这是不可能的吗?这些数据以字符串形式出现,我将这些数据应用于UIWebview。@mturhan55:“我只需要…”从来都不是在装运应用程序中使用正则表达式解析HTML的有效理由。遗憾的是,iOS上没有WebKit DOM API;如果没有它,使用JavaScript操作DOM是两种破解中的较小一种。感谢您的详细回复。首先,我的HTML数据太大了。不仅有一行@“img width=500 height=400”“。在执行我时,您的代码工作正常,但如果示例文本为“一些标记bla bla一些标记img width=500 height=400和更多标记”,则替换无效。其次,我需要将“img width=500 height=400”替换为“img width=30”.Height键和值必须删除。我删除了。您编辑的正则表达式工作得很好。非常感谢您,还有一个问题。我在Google上搜索了很多时间来查找objective-c和正则表达式,但我找不到关于这方面的好例子。您介意为此提供一个好的源代码吗?多年来编写Perl代码是一个代码起点!这对我有很大帮助。Apple文档在
上,NSRegularExpression
也不错。
<html>
    <body>
        <img width=30>
        <img width=30>
    </body>
</html>
NSArray* ary = [oldHtml componentsSeparatedByString:@"<img"];
NSString* newHtml = [ary objectAtIndex:0];
for (int i = 1; i < [ary count]; i++) {

    newHtml = [newHtml stringByAppendingString:[@"<img width=300 " stringByAppendingString:[[ary objectAtIndex:i] substringFromIndex:[[ary objectAtIndex:i] rangeOfString:@"src"].location]]];

}