开发者

Why NSRegularExpression says that there are two matches of ".*" in the "a" string?

开发者 https://www.devze.com 2023-03-23 03:45 出处:网络
I\'m very happy that Lion introduced NSRegularExpression, but I can\'t understand why the pattern .* matches two occurrences in a string like \"a\" (text can be longer).

I'm very happy that Lion introduced NSRegularExpression, but I can't understand why the pattern .* matches two occurrences in a string like "a" (text can be longer).

I was using following code:

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]);

Output from the console is:

开发者_JAVA百科

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

Can anyone explain why that is?


The regular expression .* matches zero or more characters. Thus, it will match the empty string as well as a and as such there are two matches.


Mildly surprised that it didn't match 3 times. One for the "" before the "a", one for the "a" and one for the "" after the "a".

As has been noted, use a more precise pattern; including anchors (^ and/or $) might also change the behaviour.

No-one has asked, but why would you want to do this anyway?


The documents on NSRegularExpression say the following:

Some regular expressions [...] can successfully match a zero-length range, so the comparison of the resulting range with {NSNotFound, 0} is the most reliable way to determine whether there was a match or not.

I more reliable way to get just one match would be to change the expression to .+

0

精彩评论

暂无评论...
验证码 换一张
取 消