Simple regex question. I have a string on the following format:
[page]
Some text with multi line.
[page/]
[page]
Another text with multi line.
[page/]
[page]
Third text with multi line.
[page/]
What is the regular expression to extract the text between [page]
and [page/]
?
I am using this code, but I only got the first match.
NSString *path = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"txt"];
NSString *mainText = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSError *error = NULL;
NSRange range = NSMakeRange(0, mainText.length);
NSString *pattern = [NSString stringWithFormat:@"(?<=\\[page])(?s)(.*?)(?=\\[page/])"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:mainText options:0 range:range];
if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
NSString *substringForFirstMatch = [mainText substringWithRange:rangeOfFirstMatch];
NSLog(@"sub: %@", substringForFirstMatch);
}
How can I but the text of every matches in NSArray?