NSReglar ExpressionとNSPredicateの使い方
8064 ワード
NSReglar ExpressionとNSPredicateはクエリーマッチング文字列処理であるが、NSPredicateはSQL文と同様のクエリーをサポートするクラスSQLタイプのクエリーであり、NSDateなどのNSPredicateがNSReglar Expressionに完全に置換できるとは限らない。
1.NSReglar Expressionの使い方
公式Doc:NSReglar Expression
次の例では、Web写真の収集
公式Doc:NSPredicate
1.NSReglar Expressionの使い方
公式Doc:NSReglar Expression
次の例では、Web写真の収集
NSError *error;
NSString *strRegex = @"<img([^<]+)(/>|</img>)";
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:strRegex options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators error:&error];
// .
NSArray *matches = [reg matchesInString:@" " options:NSMatchingCompleted range:NSMakeRange(0, [muStrHTMLContent length])];
for (NSTextCheckingResult *match in matches) {
i++;
NSRange range = [match range];
NSLog(@"%d,%d,%@",range.location,range.length,[muStrCloneHTMLContent substringWithRange:range]);
}
--------------------------------------------------------------------------------------------------------------------
// ,
NSString *urlString=@"<meta/><link/><title>1Q84 BOOK1</title></head><body>";
//NSRegularExpression NSError 。
NSError *error; //error ,
//http+:[^\\s]* 。(?<=title\>).*(?=</title) html <title></title>
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=title\\>).*(?=</title)" options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch=[regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
// urlString
NSString *result=[urlString substringWithRange:resultRange];
//
NSLog(@"->%@<-",result);
}
}
もちろん次のようにもできます。__block NSUInteger count = 0;
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
NSRange matchRange = [match range];
NSRange firstHalfRange = [match rangeAtIndex:1];
NSRange secondHalfRange = [match rangeAtIndex:2];
if (++count >= 100) *stop = YES;
}];
--------------------------------------------------------------------------------------
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
NSString *substringForFirstMatch = [string substringWithRange:rangeOfFirstMatch];
}
2.NSPredicateの使い方公式Doc:NSPredicate
Cocoa NSPredicate ,
:
//
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"];
BOOL match = [predicate evaluateWithObject: car];
NSLog (@"%s", (match) ? "YES" : "NO");
// cars
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
NSArray *cars = [garage cars];
for (Car *car in [garage cars]) {
if ([predicate evaluateWithObject: car]) {
NSLog (@"%@", car.name);
}
}
//
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower > 150"];
NSArray *results;
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//
NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];
NSDictionary *varDict;
varDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Herbie", @"NAME", nil];
predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
NSLog(@"SNORGLE: %@", predicate);
match = [predicate evaluateWithObject: car];
NSLog (@"%s", (match) ? "YES" : "NO");
// $VARIABLE ,
// c
predicate = [NSPredicate predicateWithFormat:
@"(engine.horsepower > 50) AND (engine.horsepower < 200)"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"oop %@", results);
predicate = [NSPredicate predicateWithFormat: @"name < 'Newton'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
//
predicate = [NSPredicate predicateWithFormat:
@"engine.horsepower BETWEEN { 50, 200 }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
NSArray *betweens = [NSArray arrayWithObjects:
[NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"];
varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil];
predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//IN
predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
names = [cars valueForKey: @"name"];
predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];
results = [names filteredArrayUsingPredicate: predicate];// SELF
NSLog (@"%@", results);
//BEGINSWITH,ENDSWITH,CONTAINS
// ,[c],[d],[cd],c ,d ,cd
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//LIKE ( )
predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
NSDateにマッチ// :
NSDate *endDate = [[NSDate date] retain];
NSTimeInterval timeInterval= [endDate timeIntervalSinceReferenceDate];
timeInterval -=3600*24*10;
NSDate *beginDate = [[NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval] retain];
// coredata ( fetchRequest)
NSPredicate *predicate_date =
[NSPredicate predicateWithFormat:@"date >= %@ AND date <= %@", beginDate,endDate];
[fetchRequest setPredicate:predicate_date];
// retained
[endDate release];
[beginDate release];