递归和正则法则
递归:
1 1 2 3 5 8 13 21 34
1 2 3 4 5 6 7 8 9
function dg(n){
if(n==1||n==2){
return 1;
}else{
return dg(n - 1) + dg(n - 2);
}
}
alert(dg(7));
正则法则:
//邮箱
- (BOOL) validateEmail:(NSString *)email
{
NSString *emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTestevaluateWithObject:email];
}
//手机号码验证
- (BOOL) validateMobile:(NSString *)mobile
{
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex =@"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTestevaluateWithObject:mobile];
}
//车牌号验证
- (BOOL) validateCarNo:(NSString *)carNo
{
NSString *carRegex =@"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
NSPredicate *carTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",carRegex];
NSLog(@"carTest is %@",carTest);
return [carTestevaluateWithObject:carNo];
}
//车型
- (BOOL) validateCarType:(NSString *)CarType
{
NSString *CarTypeRegex =@"^[\u4E00-\u9FFF]+$";
NSPredicate *carTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
return [carTestevaluateWithObject:CarType];
}
//用户名
- (BOOL) validateUserName:(NSString *)name
{
NSString *userNameRegex =@"^[A-Za-z0-9]{6,20}+$";
NSPredicate *userNamePredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",userNameRegex];
BOOL B = [userNamePredicateevaluateWithObject:name];
return B;
}
//密码
- (BOOL) validatePassword:(NSString *)passWord
{
NSString *passWordRegex =@"^[a-zA-Z0-9]{6,20}+$";
NSPredicate *passWordPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",passWordRegex];
return [passWordPredicateevaluateWithObject:passWord];
}
//昵称
- (BOOL) validateNickname:(NSString *)nickname
{
NSString *nicknameRegex =@"^[\u4e00-\u9fa5]{4,8}$";
NSPredicate *passWordPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
return [passWordPredicateevaluateWithObject:nickname];
}
//身份证号
- (BOOL) validateIdentityCard: (NSString *)identityCard
{
BOOL flag;
if (identityCard.length <= 0) {
flag = NO;
return flag;
}
NSString *regex2 =@"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicateevaluateWithObject:identityCard];
}