他の人が投稿しているとおりhere見られるように、Appleのドキュメントを詳細に[NSString isLike:]
の振る舞いを記述していない:
のデフォルト実装NSObjectメソッドによって提供されるこのメソッドはNOを返します。 NSStringは、このメソッドの実装も提供します。このメソッドは、受信者がobjectで記述されたパターンと一致する場合にYESを返します。
他にも、NSPredicate
に基づいている可能性があります。その場合hereに記載されているように、それは、おそらくオペレータタイプNSLikePredicateOperatorType
とNSComparisonPredicate
を使用している:
NSMatchesPredicateOperatorType
述部に一致する完全な正規表現。
OS X v10.4以降で使用できます。
NSLikePredicateOperatorType
SQL LIKEの挙動に類似MATCHES述語の単純なサブセット。
OS X v10.4以降で使用できます。
機能は単純な正規表現のサブセットであるかもしれませんが、構文はまったく異なります。私は以下を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:
は、ファイルが特殊な解釈を逃れるために、バックスラッシュ\
とグロブのよう?
と*
をサポートするために表示されます。また、[
と]
の文字クラスをサポートし、範囲は-
です。バックスラッシュを使用して[
を開き、バックスラッシュを使用して]
と-
をエスケープします。
[NSPredicate'の 'LIKE'演算子の動作]と一致しますか?(https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html) #// apple_ref/doc/uid/TP40001795-215868)? –
@JoshCaswell '*'はそこに記述されているように動作しますが、部分文字列の比較もある程度行っています... "FooBar" isLike "Foo" – Earlz
@Earlz、私は '[@" FooBar "isLike:@" Foo "]しかし、偽の結果を得た。 – cdlane