Trying to get the URL out of some HTML I’m parsing (on iPhone) using ‘capturing parentheses’ to just group the bit I’m interested in.
I now have this:
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]);
}
The only thing it lists is the full match (so: href=”http://tralalala.com” instead of http://tralalala.com
How can I force it to only return my first capturing parentheses match?
Regex groups work by capturing the whole match in group 0, then all groups in the regex will start at index 1.
NSTextCheckingResultstores these groups as ranges. Since your regex requires at least one group the following will work.