From looking at the docs for NSTextCheckingResult
I was under the impression that if no match in a NSRegularExpression
search was found the range property for NSCheckingResult
would be set to {NSNotFound,0}
From my test below I am finding that if no match is found NSCheckingResult
range is set to {0,0}
. Its a small point, but I just wanted to clarify my understanding of how this is working.
// REGEXPRESSION
NSString *textBuffer = @"1234567890";
NSString *pattern = @"(([A-Z]+))";
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSTextCheckingResult *match = [regExp firstMatchInString:textBuffer options:0 range:NSMakeRange(0, [textBuffer length])];
// ERROR CHECK
if([match range].location == NSNotFound) NSLog(@"Match Not found");
NSLog(@"location: %d", [match range].location);
NSLog(@"length : %d", [match range].length);
// OUTPUT
location: 0
length : 0
EDIT:
In this example NSTextCheckingResult *match
is being set to nil
, which is probably why location and length are returning zero (message to nil object).
if(!match) NSLog(@"Match Not Found");
I am therefore guessing that NSNotFound
is only returned when there are multiple capture groups where it represent an empty group.
Yes it's because of the null match. {NSNotFound, 0}
can be returned by rangeAtIndex:
for the group which didn't participate in the match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With