I have the following code:
NSString *text = @"http://bit.ly/111 http://bit.ly/222 http://www.www.www";
NSRegularExpression *aLinkRegex = [NSRegularExpression regularExpressionWithPattern:@".*http://.*" options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [aLinkRegex numberOfMatchesInString:text options:0 range:NSMakeRange(0, [text length])];
I want to find the number of http’s in the text (I know this isn’t a good regex), but numberOfMatchesInString always returns 1, while it should return 3 in the above code.
Could someone please tell me what’s wrong with the above code?
Cheers,
There is only one match, because your regular expression matches the first
http://and the.*“eats” the rest of the string.Why not search for something more like:
or if you are trying to capture each URL in full, something like:
Which means search for anything after http:// that is not a space.
You should really look into reading through some kind of regular expression guide.