Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 按对象限制NSRegularExpression_Ios_Objective C_Regex_Nsregularexpression - Fatal编程技术网

Ios 按对象限制NSRegularExpression

Ios 按对象限制NSRegularExpression,ios,objective-c,regex,nsregularexpression,Ios,Objective C,Regex,Nsregularexpression,目前,我正在使用NSRegularExpression从rss输出中提取图像。这就是我使用的: for (NSDictionary *story in stories) { NSString *string = [story objectForKey:@"content:encoded"]; NSRange rangeOfString = NSMakeRange(0, string.length); NSString *pattern = @"<img\\s[

目前,我正在使用NSRegularExpression从rss输出中提取图像。这就是我使用的:

for (NSDictionary *story in stories) {

    NSString *string = [story objectForKey:@"content:encoded"];

    NSRange rangeOfString = NSMakeRange(0, string.length);

    NSString *pattern = @"<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>";
    NSError* error = nil;

    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSArray *matchs = [regex matchesInString:[story objectForKey:@"content:encoded"] options:0 range:rangeOfString];
    for (NSTextCheckingResult* match in matchs) {
        NSLog(@"url: %@", [string substringWithRange:[match rangeAtIndex:1]]);
    }


}
这项工作真的很好,除了我只需要一个图片链接每个关键的故事,即使是这样的情况下,这是一个多个图像每个关键

我怎样才能解决这个问题


谢谢

听起来您在这里需要做的就是更换您的匹配循环:

为此:

if([matchs count] > 0)
{
    [storyMatches addObject: [string substringWithRange:[match rangeAtIndex:1]]];
}
在开始循环故事之前,请确保声明故事匹配可变数组:

换句话说,您的代码应该如下所示:

NSMutableArray *storyMatches = [[NSMutableArray alloc] initWithCapacity:[stories count]];

for (NSDictionary *story in stories) {

    NSString *string = [story objectForKey:@"content:encoded"];

    NSRange rangeOfString = NSMakeRange(0, string.length);

    NSString *pattern = @"<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>";
    NSError* error = nil;

    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSArray *matchs = [regex matchesInString:[story objectForKey:@"content:encoded"] options:0 range:rangeOfString];
    if([matchs count] > 0)
    {
        [storyMatches addObject:[string substringWithRange:[match rangeAtIndex:1]]];
    }
}

NSLog(@"storyMatches count is %d and contents is %@", [storyMatches count], [storyMatches description]);

它给了我这个错误“NSArray”的No visible@interface声明选择器“rangeAtIndex:”match不是一个NSArray对象,而是一个NSTextCheckingResult,对吗?Michael,我的错,我没有用正确的方式替换代码。它仍然打印17个对象,而不是10个。有10个故事还是17个故事?所以即使使用我的代码,你也有10个故事,但出现了17个匹配?
NSMutableArray *storyMatches = [[NSMutableArray alloc] init];
NSMutableArray *storyMatches = [[NSMutableArray alloc] initWithCapacity:[stories count]];

for (NSDictionary *story in stories) {

    NSString *string = [story objectForKey:@"content:encoded"];

    NSRange rangeOfString = NSMakeRange(0, string.length);

    NSString *pattern = @"<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>";
    NSError* error = nil;

    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
    NSArray *matchs = [regex matchesInString:[story objectForKey:@"content:encoded"] options:0 range:rangeOfString];
    if([matchs count] > 0)
    {
        [storyMatches addObject:[string substringWithRange:[match rangeAtIndex:1]]];
    }
}

NSLog(@"storyMatches count is %d and contents is %@", [storyMatches count], [storyMatches description]);