I have an NSString that comes from an HTML page that is 37014 characters long and has about 20 URLs scattered in it. I use this code to find the URLs and place them in an NSArray:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*\\.(?:jpg|gif|png))(?:\\?([^#]*))?(?:#(.*))?"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSArray *matches = [regex matchesInString:final
options:0
range:NSMakeRange(0, [final length])];
The problem is that the code takes a good 5 seconds to execute even on an iPhone 4S. It’s just too slow for one page… and I need to go through 20 HTML pages!
Is there a faster way? Perhaps NSScanner or NSRange? Anyone have any creative ideas?
Look into NSDataDetector with
NSTextCheckingTypeLink.