NSCheckingResult范围属性未设置为{NSNotfound,0}?

fuz*_*oat 6 regex iphone cocoa-touch objective-c nsregularexpression

通过查看文档,NSTextCheckingResult我认为如果找不到NSRegularExpression搜索中的匹配项,NSCheckingResult则将设置范围属性设置为{NSNotFound,0}

根据我在下面的测试,我发现如果找不到匹配,则将NSCheckingResult范围设置为{0,0}.这是一个小问题,但我只是想澄清一下我对它是如何工作的理解.

// 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
Run Code Online (Sandbox Code Playgroud)

编辑:在此示例NSTextCheckingResult *match中设置为nil,这可能是位置和长度返回零(消息到nil对象)的原因.

if(!match) NSLog(@"Match Not Found");
Run Code Online (Sandbox Code Playgroud)

因此我猜测NSNotFound只有当有多个捕获组时它才会返回,它代表一个空组.

hoh*_*oha 2

是的,这是因为空匹配。未参加比赛的组{NSNotFound, 0}可退回。rangeAtIndex: