views:

16

answers:

1

I'm trying to get some info out of a document. I'm trying to match the info I need with regex, which matches 3 numbers within a string. It works fine, but it only matches the first occurance. I need it to match an unlimited number of times because I don't know how many times this string will occur.

NSString *regex = @"String containing data:(\\d+) and more data:(\\d+) and so on";
NSArray *captures = [document captureComponentsMatchedByRegex:regex];
for(NSString *match in captures){
    NSLog(@"%@",match);
}

The above code prints out 3 strings - The entire string, the first data and the second data. All good, but now I need it to keep searching the document, because similar strings will occur n times.

How do I do this? And is there any way to group the matches into an array for each string or something like that?

+2  A: 

Use the arrayOfCaptureComponentsMatchedByRegex: method. That will return an NSArray of NSArray objects, and each nested NSArray object will have the captures (index 0 being the string, index 1 being the first capture, etc).

Dave DeLong
Great. It was that easy, thank you! :)
Accatyyc