Objective c 为什么NSRegularExpression在所有情况下都不尊重捕获组?

Objective c 为什么NSRegularExpression在所有情况下都不尊重捕获组?,objective-c,regex,nsregularexpression,Objective C,Regex,Nsregularexpression,主要问题:当我的模式是,@\\b(\\S+\\b)时,ObjC可以告诉我有六个匹配项,但当我的模式是@“ab(c)或(d)时,它只报告一个匹配项,“c” 解决方案 这里有一个函数,它将捕获组作为NSArray返回。我是一个Objective C新手,因此我怀疑有更好的方法来完成繁重的工作,而不是创建一个可变数组并在最后将其分配给NSArray - (NSArray *)regexWithResults:(NSString *)haystack pattern:(NSString *)strPat

主要问题:当我的模式是,
@\\b(\\S+\\b)
时,ObjC可以告诉我有六个匹配项,但当我的模式是
@“ab(c)或(d)
时,它只报告一个匹配项,
“c”

解决方案 这里有一个函数,它将捕获组作为NSArray返回。我是一个Objective C新手,因此我怀疑有更好的方法来完成繁重的工作,而不是创建一个可变数组并在最后将其分配给NSArray

- (NSArray *)regexWithResults:(NSString *)haystack pattern:(NSString *)strPattern
{
    NSArray *ar;
    ar = [[NSArray alloc] init];
    NSError *error = NULL;
    NSArray *arTextCheckingResults;
    NSMutableArray *arMutable = [[NSMutableArray alloc] init];
    NSRegularExpression *regex = [NSRegularExpression
        regularExpressionWithPattern:strPattern
        options:NSRegularExpressionSearch error:&error];

    arTextCheckingResults = [regex matchesInString:haystack
        options:0
        range:NSMakeRange(0, [haystack length])];

    for (NSTextCheckingResult *ntcr in arTextCheckingResults) {
        int captureIndex;
        for (captureIndex = 1; captureIndex < ntcr.numberOfRanges; captureIndex++) {
            NSString * capture = [haystack substringWithRange:[ntcr rangeAtIndex:captureIndex]];
            //NSLog(@"Found '%@'", capture);
            [arMutable addObject:capture];
        }
    }

    ar = arMutable;
    return ar;
}
该代码将产生:

That sentence had 'words' in it. 调用该测试函数,结果显示它能够计算字符串中的字数:

NSString *searchText = @"This sentence has words in it.";
[myClass regexTest:searchText pattern:@"\\b(\\S+)\\b"];
结果:

Pattern: '.*This (sentence) has (words) in it.*' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' Pattern: '.*This (\S+) has (\S+) in it.*' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' Pattern: '^This (\S+) .* (\S+) in it.$' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' 结果:

Pattern: '.*This (sentence) has (words) in it.*' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' Pattern: '.*This (\S+) has (\S+) in it.*' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' Pattern: '^This (\S+) .* (\S+) in it.$' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' 结果:

Pattern: '.*This (sentence) has (words) in it.*' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' Pattern: '.*This (\S+) has (\S+) in it.*' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' Pattern: '^This (\S+) .* (\S+) in it.$' Search text: 'This sentence has words in it.' Number of matches: 1 Found string 'sentence' 模式:“^This(\S+).*(\S+)在其中。$” 搜索文本:“这句话里有单词。” 匹配数:1 找到字符串“句子” 参考资料:

更改
NSTextCheckingResult

- (void)regexTest:(NSString *)haystack pattern:(NSString *)strPattern
{
    NSError *error = NULL;
    NSArray *arTextCheckingResults;

    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:strPattern
                                  options:NSRegularExpressionSearch
                                  error:&error];
    NSRange stringRange = NSMakeRange(0, [haystack length]);
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:haystack
                                                        options:0 range:stringRange];

    NSLog(@"Number of matches for '%@' in '%@': %u", strPattern, haystack, numberOfMatches);

    arTextCheckingResults = [regex matchesInString:haystack options:NSRegularExpressionCaseInsensitive range:stringRange];

    for (NSTextCheckingResult *ntcr in arTextCheckingResults) {
        NSRange matchRange = [ntcr rangeAtIndex:1];
        NSString *match = [haystack substringWithRange:matchRange];
        NSLog(@"Found string '%@'", match);
    }
}
NSLog输出:
找到字符串“words”

我想如果您更改

// returns the range which matched the pattern
NSString *match = [haystack substringWithRange:ntcr.range];

对于包含单个捕获的模式,您将获得预期的结果

有关NSTextCheckingResult:RangeAtinex:

结果必须至少有一个范围,但也可以有更多范围(例如,表示正则表达式捕获组)


传递RangeAtinex:值0始终返回range属性的值。其他范围(如果有)的索引范围为1到numberOfRanges-1。

这也可能与我如何使用NSTextCheckInResult有关。为什么要在以下几行中使用NSRegularExpressionSearch?NSRegularExpression*regex=[NSRegularExpression regular expression with pattern:strPattern选项:NSRegularExpression搜索错误:&error];它对以下调用是否有任何影响?arTextCheckingResults=[regex MatchesInstalling:haystack选项:nsregularexpressionCase不敏感范围:stringRange]
regex
是正则表达式,用于生成
arTextCheckingResults
。是的,但NSRegularExpressionSearch不是NSRegularExpressionOptions类型,且sregularexpressionoptions中的枚举都不是NSRegularExpressionSearch。
// returns the range of the first capture
NSString *match = [haystack substringWithRange:[ntcr rangeAtIndex:1]];