2011-12-17 4 views
1

URL 패턴과 일치 시키려고하지만 일치하지 않습니다. 기본적으로 야후의 모든 것이 허용되므로 예를 들어 ca.yahoo.com이 허용됩니다. 그것은 그 일을하지만, 단순히 yahoo.com에서는 작동하지 않습니다.NSPredicate matching

NSString *string = @"http://yahoo.com/"; 
NSString *regex = @"^http://.*\\.yahoo.com/.*$"; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[C] %@", regex]; 
BOOL match = [predicate evaluateWithObject:string]; 

NSLog(@"%@", match? @"MATCH" : @"NO LUCK"); 

인쇄 NO LUCK

답변

1

문제는 직전 야후 기간이 선택되지 않는 것입니다 : 여기에 내 코드입니다. 간단한 방법이 rangeOfString:을 사용하고 여기에

NSString *regex = @"^http://(.*\\.)*yahoo.com/.*$"; 

:

NSString *string = @"http://yahoo.com/"; 
NSString *regex = @"^http://(.*\\.)*yahoo.com/.*$"; 

NSRange range = [string rangeOfString:regex options:NSRegularExpressionSearch]; 
NSLog(@"%@", range.location!=NSNotFound? @"MATCH" : @"NO LUCK"); 

NSLog 출력 :

MATCH

다음 옵션 위의 전체를 만드는 버전입니다