- 對(duì)NSArray過(guò)濾
NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil];
NSString *string = @"ang";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];
NSLog(@"%@",[array filteredArrayUsingPredicate:pred]);
2)判斷字符串首字母是否為字母:
NSString *regex = @"^[A-Z]+$";
NSString *string = @"AdddadADa";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
NSString *stringS = [string substringToIndex:1];
NSLog(@"打印 == %@ %d",stringS,[predicate evaluateWithObject:stringS]);
3)各種驗(yàn)證
//1.0 正則驗(yàn)證(通用) regex 正則表達(dá)式 返回值:驗(yàn)證結(jié)果
- (BOOL)regular:(NSString *)regex {
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.1 驗(yàn)證電話號(hào)碼
- (BOOL)checkTelephoneNumber {
NSString *regex = @"^1[3|4|5|7|8][0-9]\\d{8}$";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.2 驗(yàn)證身份證
- (BOOL)checkIDCard {
NSString *regex = @"\\d{15}(\\d\\d[0-9xX])?";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.3 驗(yàn)證郵箱
- (BOOL)checkEmail {
NSString *regex = @"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.4 驗(yàn)證純數(shù)字
- (BOOL)checkJustNumber {
NSString *regex = @"^[0-9]+$";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.5 驗(yàn)證URL
- (BOOL)checkURL {
NSString *regex = @"^http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.6 驗(yàn)證只是漢字
- (BOOL)checkJustChinese {
NSString *regex = @"^[\u4e00-\u9fa5]+$";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.7 驗(yàn)證只是字母
- (BOOL)checkJustLetter {
NSString *regex = @"^[A-Za-z]+$";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.8 驗(yàn)證只是小寫(xiě)字母
- (BOOL)checkJustLowercase {
NSString *regex = @"^[a-z]+$";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.9 驗(yàn)證只是大寫(xiě)字母
- (BOOL)checkCapitalLetter {
NSString *regex = @"^[A-Z]+$";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}
//1.10 驗(yàn)證包含特殊字符
- (BOOL)checkContainSpecialCharacter {
NSString *regex = @"[~`!@#$%^&*':;\"\?=/<>,\\.\\{\\}\\[\\]\\(\\)]+";
NSPredicate *predicateRe = [NSPredicate predicateWithFormat:@"self matches %@", regex];
return [predicateRe evaluateWithObject:self];
}