后台车牌认证,需要前段代码先过滤一遍,不搜不知道,一搜吓一跳。原来我天朝的车牌号也是“乱七八糟五花八门”
:
-(BOOL)isCarNumber:(NSString*)num{
NSString *regNormal = @"^[京|津|沪|渝|冀|豫|云|辽|黑|湘|皖|鲁|新|苏|浙|赣|鄂|桂|甘|晋|蒙|陕|吉|闽|贵|粤|青|藏|川|宁|琼]{1}[a-zA-Z]{1}[a-zA-Z0-9]{4}[a-zA-Z0-9挂学警港澳]{1}$";
NSString * regGreen =@"^[京|津|沪|渝|冀|豫|云|辽|黑|湘|皖|鲁|新|苏|浙|赣|鄂|桂|甘|晋|蒙|陕|吉|闽|贵|粤|青|藏|川|宁|琼]{1}[a-zA-Z]{1}[a-zA-Z0-9]{6}$";
NSString *regPolice = @"^[Ww]{1}[Jj]{1}[京|津|沪|渝|冀|豫|云|辽|黑|湘|皖|鲁|新|苏|浙|赣|鄂|桂|甘|晋|蒙|陕|吉|闽|贵|粤|青|藏|川|宁|琼]{1}[0-9]{4}[XBTSHJDxbtshjd0-9]{1}$";
NSString *regArmy = @"^[VZHKEBSLJNGCvzhkebsljngc]{1}[ABCDEFGHSJLKMNORTUYZabcdefghsjlkmnortuyz]{1}[0-9]{4,5}$";
NSString *regZGZA = @"^[Z]{1}[GAga]{1}([.*])$";
if ([self isCarNum:num pred:regNormal]||[self isCarNum:num pred:regGreen]||[self isCarNum:num pred:regPolice]||[self isCarNum:num pred:regArmy]||[self isCarNum:num pred:regZGZA]) {
return YES;
}else{
return NO;
}
}
-(BOOL)isCarNum:(NSString*)num pred:(NSString*)pre{
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pre];
return [regextestmobile evaluateWithObject:num];
}
注:只能检测车牌号格式是否正确,并不能判断车牌号的存在是否:譬如:豫Z52101.判断格式是正确的,但是在我大中原是没有豫Z这个车牌简称城市的。。。如有需求,请自觉搜索每个省内的各个城市的简称字母。然后自加判断即可!