0

我目前正在自学 NSRegularExpressions 以及如何从 RSS 提要中过滤某些内容。特别是,RSS 提要的格式为“some text ::(这里可以有一个 Re: 用于回复)some text :: some text”。我想删除那个 Re: 如果它存在的话。我知道应该有一种方法可以做到这一点,而无需在我目前拥有的一个中创建另一个 NSRegularExpression。我没有掌握所有的符号。我试图使用 ?: 从捕获中取消包含 Re: ,但我不太清楚如何。有人介意帮我看看这个并帮我一把吗?

NSRegularExpression *reg = [[NSRegularExpression alloc] initWithPattern:@".* :: ?:Re: (.*) :: .*" options:0 error:nil]; //The () creates a capture group and an array of ranges for reg

    //Loop through every title of the items in channel
    for (RSSItem *i in items) {
        NSString *itemTitle = [i title];
        //find mittts
        //Find matches in the title string. The range argument specifies how much of the title to search; in this case, all of it.
        NSArray *matches = [reg matchesInString:itemTitle options:0 range:NSMakeRange(0, [itemTitle length])];

        //If there was a match...
        if ([matches count] > 0) {
             //Print the location of the match in the string and the string
            NSTextCheckingResult *result = [matches objectAtIndex:0];

            NSRange r = [result range];

            NSLog(@"\nMatch at {%d, %d} for %@!\n", r.location, r.length, itemTitle);


            NSLog(@"Range : %d",[result numberOfRanges]);
            //One capture group, so two ranges, let's verify
            if ([result numberOfRanges] == 2) {
                //Pull out the 2nd range, which will be the capture group
                NSRange r = [result rangeAtIndex:1];

                //Set the title of the item to the string within the capture group
                [i setTitle:[itemTitle substringWithRange:r]];

                NSLog(@"%@", [itemTitle substringWithRange:r]);
            }
        }
    }
4

1 回答 1

0

没关系,找到了。我需要 (?:Re: )?以防止捕获它。

于 2012-10-17T23:03:58.630 回答