简体   繁体   中英

NSPredicate use in iOS

Can anyone knows how to use NSPredicate for below format?

[Any letter][Any Number][Any letter][space][Any Number][Any letter][Any Number]

I want to validate string fot above fromat.

Thanks.

Use this format.

NSString *str1 = @"a8D 9k3";
NSString *str2 = @"a8 9k3";
NSString *testFormat = @"[a-zA-z][0-9][a-zA-z] [0-9][a-zA-z][0-9]";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", testFormat];

Following is valid :

BOOL isValid = [predicate evaluateWithObject:str1];

Following is invalid :

BOOL isValid = [predicate evaluateWithObject:str2]; 
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", @"[a-z][0-9][a-z] [0-9][a-z][0-9]"];

if ([pred evaluateWithObject:@"a3B 5C9"])
{
    NSLog(@"It matches!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM