I have the following string: "Title: " and I am using regex in Objective-C to extract the "/books/1/title" part from the string. (The string can contain multiple expressions) The regex is as follows <\?(.+?)\?>. My problem is that it matches the whole string(from index 0 to 24) and not the content between the tags. The code is as follows:
NSString *object = @"Title: <?/books/1/title?>";
NSMutableString *newString = [[NSMutableString alloc] initWithString:object];
NSString *pattern = @"<\?(.+?)\?>";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:0 error:NULL];
NSArray *matches = [regex matchesInString:object options:0 range:NSMakeRange(0, [object length])];
for (NSInteger i = [matches count]-1; i>=0 ; i--) {
NSTextCheckingResult * match = [matches objectAtIndex:i];
if (match != nil && [match rangeAtIndex:0].location != NSNotFound && [match numberOfRanges] == 2) {
NSRange part1Range = [match rangeAtIndex:1];
NSLog(@"%lu %lu", (unsigned long)part1Range.location, (unsigned long)part1Range.length);
}
}