本文主要是介绍IOS 常用正则表达式整理(邮箱、身份证号、固化、手机号、纯数字、纯文字等),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言:常用正则表达式整理,包括邮箱、身份证号、固化、手机号、纯数字、纯文字等
一、对邮箱进行校验
+ (BOOL)checkEmail:(NSString *)email
{NSString *emailReg = @"^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailReg];if ([regextestmobile evaluateWithObject:email] == YES){return YES;}return NO;
}
+ (BOOL)checkPersonID:(NSString *)personID
{NSString *person = @"(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",person];if ([regextestmobile evaluateWithObject:personID] == YES){return YES;}return NO;
}
三、对固话进行校验
#pragma mark 检验是否是固定电话
+ (BOOL)checkLandlinePhone:(NSString *)landlinePhone
{NSString *mobile = @"0\\d{2,3}-\\d{5,9}|0\\d{2,3}-\\d{5,9}";NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobile];if ([regextestmobile evaluateWithObject:landlinePhone] == YES){return YES;}return NO;
}
四、对手机号进行校验
#pragma mark 检测手机号码是否正确
+ (BOOL)checkMobileNumber:(NSString *)mobileNum{mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@"-" withString:@""];mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@" " withString:@""];if ([mobileNum hasPrefix:@"00"])return NO;if ([mobileNum hasPrefix:@" "]){mobileNum = [mobileNum stringByReplacingOccurrencesOfString:@" " withString:@""];}/*** 手机号码* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188* 联通:130,131,132,152,155,156,185,186* 电信:133,1349,153,180,189** 新增* 移动:152 154 178 183 184* 联通:176* 电信:177 181*/NSString * MOBILE = @"^1(3[0-9]|4[0-9]|5[0-9]|7[67]|8[0-9])\\d{8}$";/**10 * 中国移动:China Mobile11 * 134[0-8],135,136,137,138,139,150,151,152,154,157,158,159,178,182,183,184,187,18812 */NSString * CM = @"^1(34[0-8]|(3[5-9]|5[0-247-9]|7[8]|8[2-478])\\d)\\d{7}$";/**15 * 中国联通:China Unicom16 * 130,131,132,152,155,156,176,185,18617 */NSString * CU = @"^1(3[0-2]|5[256]|7[6]|8[56])\\d{8}$";/**20 * 中国电信:China Telecom21 * 133,1349,153,177,180,181,18922 */NSString * CT = @"^1((33|53|7[7]|8[019])[0-9]|349)\\d{7}$";/**25 * 大陆地区固话及小灵通26 * 区号:010,020,021,022,023,024,025,027,028,02927 * 号码:七位或八位28 */NSString *telNum = @"^0\\d[1-9]{1}(\\d[0-9]{1,2}\\d[1-9]{1})\\d{4,6}";NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];NSPredicate *regextestTel = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", telNum];if ([regextestTel evaluateWithObject:mobileNum]){MyLog(@"pass");}if (([regextestmobile evaluateWithObject:mobileNum] == YES)|| ([regextestcm evaluateWithObject:mobileNum] == YES)|| ([regextestct evaluateWithObject:mobileNum] == YES)|| ([regextestcu evaluateWithObject:mobileNum] == YES)|| ([regextestTel evaluateWithObject:mobileNum] == YES)){return YES;}return NO;
}
五、纯数字
NSString *reg = @"^\\d*$";
六、纯字母
NSString *reg = @"^[A-Za-z]*$";
七、首字母为字母,其它为A-Za-z_0-9
NSString *reg = @"^[a-zA-Z]\\w{1,}$";
这篇关于IOS 常用正则表达式整理(邮箱、身份证号、固化、手机号、纯数字、纯文字等)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!