我试图找出isLike:实际上对NSString做了什么,并且遇到了麻烦。苹果的own documentation非常模糊:



它提到了“模式”,但是经过一些初步的测试,它似乎没有使用正则表达式。在这种情况下,模式格式到底是什么?

最佳答案

正如其他人所发表的那样,Apple的文档并未像here所示详细描述[NSString isLike:]的行为:

正如其他人建议的那样,它可能基于NSPredicate。如果是这样,很可能将NSComparisonPredicate与运算符类型NSLikePredicateOperatorType一起使用,如here所述:

尽管功能可能只是正则表达式的简单子(monad)集,但语法绝对不同。我今天在OS X 10.10.5上本地测试了以下内容:

- (NSString *)escapeString:(NSString *)value {
    return [value stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
}

- (void)is:(NSString *)value like:(NSString *)pattern note:(NSString *)note {
    NSLog(@"[@\"%@\" isLike:@\"%@\"] == %@ // %@",
            [self escapeString:value],
            [self escapeString:pattern],
            ([value isLike:pattern] ? @"true" : @"false"),
            note);
}

- (void)testAll {
    // each note contains result on OS X 10.10.5 on 20160503
    [self is:@"foo" like:@"f*" note:@"true, '*' wildcard works like file globbing, not RE"];
    [self is:@"foo" like:@"foo*" note:@"true, '*' is zero or more"];
    [self is:@"foo" like:@"f?o" note:@"true, '?' wildcard works like file globbing, not RE"];
    [self is:@"foo" like:@"f?" note:@"false, not more then one"];
    [self is:@"foo" like:@"f?oo" note:@"false, not less than one"];
    [self is:@"foo" like:@"Foo" note:@"false, is case-sensitive (also see isCaseInsensitiveLike:)"];
    [self is:@"foo" like:@"[Ff]oo" note:@"true, supports character classes"];
    [self is:@"foo" like:@"[^F]oo" note:@"false, does not support RE negation in character classes"];
    [self is:@"foo" like:@"[a-z]oo" note:@"true, supports ranges"];
    [self is:@"foo" like:@"[[:lower:]]oo" note:@"false, does not support POSIX named classes"];
    [self is:@"]oo" like:@"[]]oo" note:@"false, does not support ']' as first character in a class"];
    [self is:@"]oo" like:@"[\\]]oo" note:@"true, backslash to escape interpretation of ']' as end of class"];
    [self is:@"[oo" like:@"\\[oo" note:@"true, backslash to escape interpretation as start of class"];
    [self is:@"-oo" like:@"[x\\-z]oo" note:@"true, supports escape of '-' in character classes"];
    [self is:@"?oo" like:@"\\?oo" note:@"true, escape with backslash"];
    [self is:@"foo" like:@"\\?oo" note:@"false, this is not just wildcard matching"];
    [self is:@"*oo" like:@"\\*oo" note:@"true, escape with backslash"];
    [self is:@"foo" like:@"\\*oo" note:@"false, this is not just wildcard matching"];
    [self is:@"\\foo" like:@"\\\\*oo" note:@"true, escape backslash with another backslash"];
}
这段代码会产生以下结果:
[@"foo" isLike:@"f*"] == true // true, '*' wildcard works like file globbing, not RE
[@"foo" isLike:@"foo*"] == true // true, '*' is zero or more
[@"foo" isLike:@"f?o"] == true // true, '?' wildcard works like file globbing, not RE
[@"foo" isLike:@"f?"] == false // false, not more then one
[@"foo" isLike:@"f?oo"] == false // false, not less than one
[@"foo" isLike:@"Foo"] == false // false, is case-sensitive (also see isCaseInsensitiveLike:)
[@"foo" isLike:@"[Ff]oo"] == true // true, supports character classes
[@"foo" isLike:@"[^F]oo"] == false // false, does not support RE negation in character classes
[@"foo" isLike:@"[a-z]oo"] == true // true, supports ranges
[@"foo" isLike:@"[[:lower:]]oo"] == false // false, does not support POSIX named classes
[@"]oo" isLike:@"[]]oo"] == false // false, does not support ']' as first character in a class
[@"]oo" isLike:@"[\\]]oo"] == true // true, backslash to escape interpretation of ']' as end of class
[@"[oo" isLike:@"\\[oo"] == true // true, backslash to escape interpretation as start of class
[@"-oo" isLike:@"[x\\-z]oo"] == true // true, supports escape of '-' in character classes
[@"?oo" isLike:@"\\?oo"] == true // true, escape with backslash
[@"foo" isLike:@"\\?oo"] == false // false, this is not just wildcard matching
[@"*oo" isLike:@"\\*oo"] == true // true, escape with backslash
[@"foo" isLike:@"\\*oo"] == false // false, this is not just wildcard matching
[@"\\foo" isLike:@"\\\\*oo"] == true // true, escape backslash with another backslash
因此,isLike:似乎支持?*,例如使用反斜杠\进行文件通配以逃避特殊解释。它还支持[]的字符类,其范围由-定义。反斜杠可以避免在类中打开[,反斜杠可以避免类中的]-

关于objective-c - NSString的isLike:实际上是做什么的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35902973/

10-12 22:01