gpt4 book ai didi

objective-c - 为什么 NSRegularExpression 说在 ".*"字符串中有两个 "a"的匹配项?

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:29 24 4
gpt4 key购买 nike

我很高兴 Lion 引入了 NSRegularExpression,但我不明白为什么模式 .* 匹配字符串中的两次出现,如“a”(文本可以更长)。

我正在使用以下代码:

NSError *anError = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@".*"
options:0
error:&anError];
NSString *text = @"a";
NSUInteger counter = [regex numberOfMatchesInString:text
options:0
range:NSMakeRange(0, [text length])];

NSLog([NSString stringWithFormat:@"counter = %u", counter]);

控制台的输出是:

2011-07-27 22:03:27.689 Regex[1930:707] counter = 2

谁能解释一下这是为什么?

最佳答案

正则表达式.* 匹配零个或多个 个字符。因此,它将匹配空字符串以及 a,因此有两个匹配项。

关于objective-c - 为什么 NSRegularExpression 说在 ".*"字符串中有两个 "a"的匹配项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6850619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com