通过查看 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
编辑:
在此示例中,NSTextCheckingResult *match
被设置为 nil
,这可能是位置和长度返回零(消息到 nil 对象)的原因。
if(!match) NSLog(@"Match Not Found");
因此我猜测 NSNotFound
仅在存在多个捕获组时才返回,其中它代表一个空组。
最佳答案
是的,这是因为空匹配。 {NSNotFound, 0}
可以通过 rangeAtIndex:
为没有参加比赛的组返回。
关于iphone - NSCheckingResult 范围属性未设置为 {NSNotfound,0}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9210337/