6

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • 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. – hoha Feb 09 '12 at 17:12
  • @hoha if you move your comment into an answer, then fuzzygoat could accept it and move this question out of the Unanswered queue. – Elise van Looij May 19 '12 at 14:34

1 Answers1

2

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.

hoha
  • 4,418
  • 17
  • 15