Hi I'm trying to evaluate an NSString to see if it fits a certain criteria, which contains wildcards in the form of one or more asterix characters.
Example:
NSString *filePath = @"file://Users/test/Desktop/file.txt";
开发者_StackOverflow社区NSString *matchCriteria = @"file://*/file.*";
I would like to see if filePath
matches (fits?) matchCriteria
, in this example it does.
Does anybody know how I can go about doing this?
Many thanks!
In addition to NSRegularExpression
, you can also use NSPredicate
.
NSString *filePath = @"file://Users/test/Desktop/file.txt";
NSString *matchCriteria = @"file://*/file.*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"self LIKE %@", matchCriteria];
BOOL filePathMatches = [pred evaluateWithObject:filePath];
The Predicate Programming Guide is a comprehensive document desribing how predicates work. They can be particularly useful if you have an array of items that you want to filter based on some criteria.
NSString implements these methods:
@interface NSObject (NSComparisonMethods)
- (BOOL)isLike:(NSString *)object;
- (BOOL)isCaseInsensitiveLike:(NSString *)object;
@end
They are much faster than NSPredicate and NSRegularExpression.
You can use NSRegularExpression.
精彩评论