为什么NSRegularExpression说"a"字符串中有两个".*"匹配?

Kro*_*owy 3 regex macos cocoa objective-c

我很高兴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]);
Run Code Online (Sandbox Code Playgroud)

控制台的输出是:

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

谁能解释为什么会这样?

You*_*You 6

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