1

単純なNSPredicatesと正規表現に問題があります。

NSString *mystring = @"file://questions/123456789/desc-text-here";
NSString *regex = @"file://questions+";

NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [regextest evaluateWithObject:mystring];

上記の例では、 isMatchisは常にfalse/NOです。

私は何が欠けていますか?に一致する正規表現が見つからないようですfile://questions

4

2 に答える 2

5

NSPredicatesは、部分文字列だけでなく、文字列全体と一致しようとしているようです。末尾+は、単に1つ以上の「s」文字と一致することを意味します。末尾の文字との一致を許可する必要があります。これは機能します:regex = @"file://questions.*"

于 2009-10-17T17:34:55.313 に答える
5

文字列が存在するかどうかをテストしようとしているだけの場合:これを試してください

NSString *myString = @"file://questions/123456789/desc-text-here";
NSString *searchString = @"file://questions";

NSRange resultRange = [myString rangeWithString:searchString];
BOOL result = resultRange.location != NSNotFound;

または、述語を使用します

NSString *myString = @"file://questions/123456789/desc-text-here";
NSString *searchString = @"file://questions";

NSPredicate *testPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@", searchString];

BOOL result = [testPredicate evaluateWithObject:myString];

ドキュメントには、部分文字列が存在するかどうかを確認するだけで、述語を使用する方法が記載されていると思います。

于 2009-10-17T17:37:23.407 に答える