嘗試使用「捕獲括號」剛組的位,我感興趣的是得到URL一些HTML我解析(在iPhone上)的。捕獲括號使用正則表達式對iphone
我現在有這:
NSString *imageHtml; //a string with some HTML in it
NSRegularExpression* innerRegex = [[NSRegularExpression alloc] initWithPattern:@"href=\"(.*?)\"" options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators error:nil];
NSTextCheckingResult* firstMatch = [innerRegex firstMatchInString:imageHtml options:0 range:NSMakeRange(0, [imageHtml length])];
[innerRegex release];
if(firstMatch != nil)
{
newImage.detailsURL =
NSLog(@"found url: %@", [imageHtml substringWithRange:firstMatch.range]);
}
它列出的唯一的事情就是全場比賽(這樣:HREF =「http://tralalala.com」,而不是http://tralalala.com
我怎樣才能迫使它只能退回我的第一個捕獲的括號匹配?
太棒了!這正是我所期待的。 – 2011-05-06 07:40:32