2016-03-09 8 views
23

isLike:は実際にNSStringに何が起こっているのか分かりません。 Appleのown documentationは非常にあいまいです:NSStringのisLikeは実際に何をしていますか?

は、受信機が 別の指定されたオブジェクト「のように」であるかどうかを示すブール値を返します。

...

NOを返す NSObject方法により提供されるこのメソッドのデフォルト実装。 はまた、 オブジェクトで記述されたパターンと受信者が一致する場合にYESを返すこのメソッドの実装を提供します( )。

「パターン」が記載されていますが、基本的なテストでは正規表現が使用されていないようです。この場合のパターンフォーマットはどういうものですか?

+2

[NSPredicate'の 'LIKE'演算子の動作]と一致しますか?(https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html) #// apple_ref/doc/uid/TP40001795-215868)? –

+0

@JoshCaswell '*'はそこに記述されているように動作しますが、部分文字列の比較もある程度行っています... "FooBar" isLike "Foo" – Earlz

+1

@Earlz、私は '[@" FooBar "isLike:@" Foo "]しかし、偽の結果を得た。 – cdlane

答えて

10

他の人が投稿しているとおりhere見られるように、Appleのドキュメントを詳細に[NSString isLike:]の振る舞いを記述していない:

のデフォルト実装NSObjectメソッドによって提供されるこのメソッドはNOを返します。 NSStringは、このメソッドの実装も提供します。このメソッドは、受信者がobjectで記述されたパターンと一致する場合にYESを返します。

他にも、NSPredicateに基づいている可能性があります。その場合hereに記載されているように、それは、おそらくオペレータタイプNSLikePredicateOperatorTypeNSComparisonPredicateを使用している:

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:は、ファイルが特殊な解釈を逃れるために、バックスラッシュ\とグロブのよう?*をサポートするために表示されます。また、[]の文字クラスをサポートし、範囲は-です。バックスラッシュを使用して[を開き、バックスラッシュを使用して]-をエスケープします。

8

ヘッダNSScriptWhoseTest.hは少しより多くの情報を提供しています。

@interface NSObject (NSComparisonMethods) 
... 

- (BOOL)isLike:(NSString *)object; 
    // argument should be a string using simple shell wildcards (* and ?). 
    // (e.g. "Stev*" or "N?XT"). 
    // Returns NO if receiver is not an NSString. 

- (BOOL)isCaseInsensitiveLike:(NSString *)object; 

@end 
+1

@ JoshCaswellと一緒に[NSPredicate]へのリンク(https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-215868) )、それは可能性が高い '*'と '?唯一のパターン文字です。 '\'はエスケープのためにも許されていることに注意してください: '[@" F?oo "isLike:@" \\ F \\?oo "]'は真です。 –

+0

後に、これは大部分が不完全であることが判明しました。 'isLike:" [abc] foobar "は" afoobar "と" cfoobar "の両方に対してtrueを返します。 – Earlz

関連する問題