NSString *myText = @"mary had a little lamb";
NSString *regexString = @"mary(.*?)little";
for(NSString *match in [myText captureComponentsMatchedByRegex:regexString]){
NSLog(@"%@",match);
}
This will output to the console two things:
1) “mary had a little”
2) “had a”
What I want is just the 2nd bit of information “had a”. Is there is a way of matching a string and returning just the inner part?
I’m fairly new to Objective C, this feels a rather trivial question yet I can’t find a less messy way of doing this than incrementing an integer in the for loop and on the second iteration storing the “had a” in an NSString.
With regular expressions, it’s standard that the first match returned is the whole matched string (“mary had a little”), then the next items are the captured groups (“had a”).
-captureComponentsMatchedByRegex:returns aNSArrayof the matches. So, if you want the second item, or the first captured group: