Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCheckingResult Range Property not set to {NSNotfound,0}?

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.

like image 574
fuzzygoat Avatar asked Feb 09 '12 11:02

fuzzygoat


1 Answers

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.

like image 132
hoha Avatar answered Oct 06 '22 00:10

hoha