0
我目前在教自己NSRegularExpressions以及如何過濾RSS源中的某些內容。特別是RSS feed的格式是「some text ::(可以有Re:這裏是答覆)some text :: some text」。我想刪除該Re:如果它存在。我知道應該有一種方法來做到這一點,而不是在我現有的那個中創建另一個NSRegularExpression。我沒有掌握所有的符號。我試圖使用?:取消Re:從捕獲,但我無法弄清楚如何。有人會介意爲我着想,並幫助我嗎?NSRegularExpressions - 過濾掉Xcode中的單詞
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]);
}
}
}