I wrote this NSRegularExpression for detecting Core Graphics C functions:
But it's not picking up individual arguments like I want.
For example, I want this string
to have the following subranges captured:
But instead right now it picks up:
Why is it picking up that middle comma? I set it aside in its own non-capture group explicitly so it wouldn't be picked up and placed in any of the groups.
Here's my code using the regular expression:
(It starts with i = 1 because rangeAtIndex:0 always has the range of the entire matched string, whereas the 1 through numberOfRanges - 1 are supposed to have the matches for the individual capture groups.)
Code:
NSString *regexString = @"([_a-zA-Z][_0-9a-zA-Z]*)\\(context(?:,(-?[0-9]*.?[0-9]+))*\\);";
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:regexString options:0 error:nil]
But it's not picking up individual arguments like I want.
For example, I want this string
Code:
CGContextMoveToPoint(context,0,100);
to have the following subranges captured:
Code:
CGContextMoveToPoint
0
100
But instead right now it picks up:
Code:
CGContextMoveToPoint
0,100
Why is it picking up that middle comma? I set it aside in its own non-capture group explicitly so it wouldn't be picked up and placed in any of the groups.
Here's my code using the regular expression:
Code:
[regex enumerateMatchesInString:codeString options:0 range:NSMakeRange(0, codeString.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
for (NSUInteger i = 1, n = result.numberOfRanges; i < n; i++) {
NSLog(@"%@", [codeString substringWithRange:[result rangeAtIndex:i]]);
}
}];
(It starts with i = 1 because rangeAtIndex:0 always has the range of the entire matched string, whereas the 1 through numberOfRanges - 1 are supposed to have the matches for the individual capture groups.)
Last edited: