iOS正则表达式验证手机号、邮箱、身份证号等
程序员文章站
2024-02-18 14:10:22
本文实例为大家分享了常用的ios正则表达式,供大家参考,具体内容如下
#import "nsstring+regexcategory.h"
@impleme...
本文实例为大家分享了常用的ios正则表达式,供大家参考,具体内容如下
#import "nsstring+regexcategory.h" @implementation nsstring (regexcategory) #pragma mark - 正则相关 - (bool)isvalidatebyregex:(nsstring *)regex{ nspredicate *pre = [nspredicate predicatewithformat:@"self matches %@",regex]; return [pre evaluatewithobject:self]; } #pragma mark - //手机号分服务商 - (bool)ismobilenumberclassification{ /** * 手机号码 * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705 * 联通:130,131,132,152,155,156,185,186,1709 * 电信:133,1349,153,180,189,1700 */ // nsstring * mobile = @"^1((3//d|5[0-35-9]|8[025-9])//d|70[059])\\d{7}$";//总况 /** 10 * 中国移动:china mobile 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705 12 */ nsstring * cm = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d|705)\\d{7}$"; /** 15 * 中国联通:china unicom 16 * 130,131,132,152,155,156,185,186,1709 17 */ nsstring * cu = @"^1((3[0-2]|5[256]|8[56])\\d|709)\\d{7}$"; /** 20 * 中国电信:china telecom 21 * 133,1349,153,180,189,1700 22 */ nsstring * ct = @"^1((33|53|8[09])\\d|349|700)\\d{7}$"; /** 25 * 大陆地区固话及小灵通 26 * 区号:010,020,021,022,023,024,025,027,028,029 27 * 号码:七位或八位 28 */ nsstring * phs = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$"; // nspredicate *regextestmobile = [nspredicate predicatewithformat:@"self matches %@", mobile]; if (([self isvalidatebyregex:cm]) || ([self isvalidatebyregex:cu]) || ([self isvalidatebyregex:ct]) || ([self isvalidatebyregex:phs])) { return yes; } else { return no; } } //手机号有效性 - (bool)ismobilenumber{ /** * 手机号以13、15、18、170开头,8个 \d 数字字符 * 小灵通 区号:010,020,021,022,023,024,025,027,028,029 还有未设置的新区号xxx */ nsstring *mobilenoregex = @"^1((3\\d|5[0-35-9]|8[025-9])\\d|70[059])\\d{7}$";//除4以外的所有个位整数,不能使用[^4,\\d]匹配,这里是否ios bug? nsstring *phsregex =@"^0(10|2[0-57-9]|\\d{3})\\d{7,8}$"; bool ret = [self isvalidatebyregex:mobilenoregex]; bool ret1 = [self isvalidatebyregex:phsregex]; return (ret || ret1); } //邮箱 - (bool)isemailaddress{ nsstring *emailregex = @"[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\\.[a-za-z]{2,4}"; return [self isvalidatebyregex:emailregex]; } //身份证号 - (bool) simpleverifyidentitycardnum { nsstring *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xx])$"; return [self isvalidatebyregex:regex2]; } //车牌 - (bool)iscarnumber{ //车牌号:湘k-de829 香港车牌号码:粤z-j499港 nsstring *carregex = @"^[\u4e00-\u9fff]{1}[a-za-z]{1}[-][a-za-z_0-9]{4}[a-za-z_0-9_\u4e00-\u9fff]$";//其中\u4e00-\u9fa5表示unicode编码中汉字已编码部分,\u9fa5-\u9fff是保留部分,将来可能会添加 return [self isvalidatebyregex:carregex]; } - (bool)ismacaddress{ nsstring * macaddregex = @"([a-fa-f\\d]{2}:){5}[a-fa-f\\d]{2}"; return [self isvalidatebyregex:macaddregex]; } - (bool)isvalidurl { nsstring *regex = @"^((http)|(https))+:[^\\s]+\\.[^\\s]*$"; return [self isvalidatebyregex:regex]; } - (bool)isvalidchinese; { nsstring *chineseregex = @"^[\u4e00-\u9fa5]+$"; return [self isvalidatebyregex:chineseregex]; } - (bool)isvalidpostalcode { nsstring *postalregex = @"^[0-8]\\d{5}(?!\\d)$"; return [self isvalidatebyregex:postalregex]; } - (bool)isvalidtaxno { nsstring *taxnoregex = @"[0-9]\\d{13}([0-9]|x)$"; return [self isvalidatebyregex:taxnoregex]; } - (bool)isvalidwithminlenth:(nsinteger)minlenth maxlenth:(nsinteger)maxlenth containchinese:(bool)containchinese firstcannotbedigtal:(bool)firstcannotbedigtal; { // [\u4e00-\u9fa5a-za-z0-9_]{4,20} nsstring *hanzi = containchinese ? @"\u4e00-\u9fa5" : @""; nsstring *first = firstcannotbedigtal ? @"^[a-za-z_]" : @""; nsstring *regex = [nsstring stringwithformat:@"%@[%@a-za-z0-9_]{%d,%d}", first, hanzi, (int)(minlenth-1), (int)(maxlenth-1)]; return [self isvalidatebyregex:regex]; } - (bool)isvalidwithminlenth:(nsinteger)minlenth maxlenth:(nsinteger)maxlenth containchinese:(bool)containchinese containdigtal:(bool)containdigtal containletter:(bool)containletter containothercharacter:(nsstring *)containothercharacter firstcannotbedigtal:(bool)firstcannotbedigtal; { nsstring *hanzi = containchinese ? @"\u4e00-\u9fa5" : @""; nsstring *first = firstcannotbedigtal ? @"^[a-za-z_]" : @""; nsstring *lengthregex = [nsstring stringwithformat:@"(?=^.{%@,%@}$)", @(minlenth), @(maxlenth)]; nsstring *digtalregex = containdigtal ? @"(?=(.*\\d.*){1})" : @""; nsstring *letterregex = containletter ? @"(?=(.*[a-za-z].*){1})" : @""; nsstring *characterregex = [nsstring stringwithformat:@"(?:%@[%@a-za-z0-9%@]+)", first, hanzi, containothercharacter ? containothercharacter : @""]; nsstring *regex = [nsstring stringwithformat:@"%@%@%@%@", lengthregex, digtalregex, letterregex, characterregex]; return [self isvalidatebyregex:regex]; } #pragma mark - 算法相关 //精确的身份证号码有效性检测 + (bool)accurateverifyidcardnumber:(nsstring *)value { value = [value stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; int length =0; if (!value) { return no; }else { length = (int)value.length; if (length !=15 && length !=18) { return no; } } // 省份代码 nsarray *areasarray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"]; nsstring *valuestart2 = [value substringtoindex:2]; bool areaflag =no; for (nsstring *areacode in areasarray) { if ([areacode isequaltostring:valuestart2]) { areaflag =yes; break; } } if (!areaflag) { return false; } nsregularexpression *regularexpression; nsuinteger numberofmatch; int year =0; switch (length) { case 15: year = [value substringwithrange:nsmakerange(6,2)].intvalue +1900; if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) { regularexpression = [[nsregularexpression alloc] initwithpattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$" options:nsregularexpressioncaseinsensitive error:nil];//测试出生日期的合法性 }else { regularexpression = [[nsregularexpression alloc]initwithpattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$" options:nsregularexpressioncaseinsensitive error:nil];//测试出生日期的合法性 } numberofmatch = [regularexpression numberofmatchesinstring:value options:nsmatchingreportprogress range:nsmakerange(0, value.length)]; if(numberofmatch >0) { return yes; }else { return no; } case 18: year = [value substringwithrange:nsmakerange(6,4)].intvalue; if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) { regularexpression = [[nsregularexpression alloc] initwithpattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9xx]$" options:nsregularexpressioncaseinsensitive error:nil];//测试出生日期的合法性 }else { regularexpression = [[nsregularexpression alloc] initwithpattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9xx]$" options:nsregularexpressioncaseinsensitive error:nil];//测试出生日期的合法性 } numberofmatch = [regularexpression numberofmatchesinstring:value options:nsmatchingreportprogress range:nsmakerange(0, value.length)]; if(numberofmatch >0) { int s = ([value substringwithrange:nsmakerange(0,1)].intvalue + [value substringwithrange:nsmakerange(10,1)].intvalue) *7 + ([value substringwithrange:nsmakerange(1,1)].intvalue + [value substringwithrange:nsmakerange(11,1)].intvalue) *9 + ([value substringwithrange:nsmakerange(2,1)].intvalue + [value substringwithrange:nsmakerange(12,1)].intvalue) *10 + ([value substringwithrange:nsmakerange(3,1)].intvalue + [value substringwithrange:nsmakerange(13,1)].intvalue) *5 + ([value substringwithrange:nsmakerange(4,1)].intvalue + [value substringwithrange:nsmakerange(14,1)].intvalue) *8 + ([value substringwithrange:nsmakerange(5,1)].intvalue + [value substringwithrange:nsmakerange(15,1)].intvalue) *4 + ([value substringwithrange:nsmakerange(6,1)].intvalue + [value substringwithrange:nsmakerange(16,1)].intvalue) *2 + [value substringwithrange:nsmakerange(7,1)].intvalue *1 + [value substringwithrange:nsmakerange(8,1)].intvalue *6 + [value substringwithrange:nsmakerange(9,1)].intvalue *3; int y = s %11; nsstring *m =@"f"; nsstring *jym =@"10x98765432"; m = [jym substringwithrange:nsmakerange(y,1)];// 判断校验位 if ([m isequaltostring:[value substringwithrange:nsmakerange(17,1)]]) { return yes;// 检测id的校验位 }else { return no; } }else { return no; } default: return no; } } /** 银行卡号有效性问题luhn算法 * 现行 16 位银联卡现行卡号开头 6 位是 622126~622925 之间的,7 到 15 位是银行自定义的, * 可能是发卡分行,发卡网点,发卡序号,第 16 位是校验码。 * 16 位卡号校验位采用 luhm 校验方法计算: * 1,将未带校验位的 15 位卡号从右依次编号 1 到 15,位于奇数位号上的数字乘以 2 * 2,将奇位乘积的个十位全部相加,再加上所有偶数位上的数字 * 3,将加法和加上校验位能被 10 整除。 */ - (bool)bankcardluhmcheck{ nsstring * lastnum = [[self substringfromindex:(self.length-1)] copy];//取出最后一位 nsstring * forwardnum = [[self substringtoindex:(self.length -1)] copy];//前15或18位 nsmutablearray * forwardarr = [[nsmutablearray alloc] initwithcapacity:0]; for (int i=0; i<forwardnum.length; i++) { nsstring * substr = [forwardnum substringwithrange:nsmakerange(i, 1)]; [forwardarr addobject:substr]; } nsmutablearray * forwarddescarr = [[nsmutablearray alloc] initwithcapacity:0]; for (int i = (int)(forwardarr.count-1); i> -1; i--) {//前15位或者前18位倒序存进数组 [forwarddescarr addobject:forwardarr[i]]; } nsmutablearray * arroddnum = [[nsmutablearray alloc] initwithcapacity:0];//奇数位*2的积 < 9 nsmutablearray * arroddnum2 = [[nsmutablearray alloc] initwithcapacity:0];//奇数位*2的积 > 9 nsmutablearray * arrevennum = [[nsmutablearray alloc] initwithcapacity:0];//偶数位数组 for (int i=0; i< forwarddescarr.count; i++) { nsinteger num = [forwarddescarr[i] intvalue]; if (i%2) {//偶数位 [arrevennum addobject:[nsnumber numberwithinteger:num]]; }else{//奇数位 if (num * 2 < 9) { [arroddnum addobject:[nsnumber numberwithinteger:num * 2]]; }else{ nsinteger decadenum = (num * 2) / 10; nsinteger unitnum = (num * 2) % 10; [arroddnum2 addobject:[nsnumber numberwithinteger:unitnum]]; [arroddnum2 addobject:[nsnumber numberwithinteger:decadenum]]; } } } __block nsinteger sumoddnumtotal = 0; [arroddnum enumerateobjectsusingblock:^(nsnumber * obj, nsuinteger idx, bool *stop) { sumoddnumtotal += [obj integervalue]; }]; __block nsinteger sumoddnum2total = 0; [arroddnum2 enumerateobjectsusingblock:^(nsnumber * obj, nsuinteger idx, bool *stop) { sumoddnum2total += [obj integervalue]; }]; __block nsinteger sumevennumtotal =0 ; [arrevennum enumerateobjectsusingblock:^(nsnumber * obj, nsuinteger idx, bool *stop) { sumevennumtotal += [obj integervalue]; }]; nsinteger lastnumber = [lastnum integervalue]; nsinteger luhmtotal = lastnumber + sumevennumtotal + sumoddnum2total + sumoddnumtotal; return (luhmtotal%10 ==0)?yes:no; } - (bool)isipaddress{ nsstring *regex = [nsstring stringwithformat:@"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"]; nspredicate *pre = [nspredicate predicatewithformat:@"self matches %@",regex]; bool rc = [pre evaluatewithobject:self]; if (rc) { nsarray *componds = [self componentsseparatedbystring:@","]; bool v = yes; for (nsstring *s in componds) { if (s.integervalue > 255) { v = no; break; } } return v; } return no; } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: PHP发送AT指令实例代码