From looking at the documents for NSTextCheckingResult
, I got the impression that if no match was found in the NSRegularExpression
search, the range property for NSCheckingResult
would be set to {NSNotFound,0}
From my test below, I find that if no match is found, then the NSCheckingResult
range NSCheckingResult
set to {0,0}
. This is a small point, but I just wanted to clarify my understanding of how this works.
// REGEXPRESSION NSString *textBuffer = @"1234567890"; NSString *pattern = @"(([AZ]+))"; 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
set to nil
, which is probably due to the location and length returning zero (message to the nil object).
if(!match) NSLog(@"Match Not Found");
Therefore, I assume that NSNotFound
returns only when there are several capture groups, where it represents an empty group.
source share