IOS Regular expression Verification phone number, mailbox, ID number, etc. _ios

Source: Internet
Author: User
Tags regular expression

This example for you to share the common iOS regular expressions for your reference, the details are as follows

#import "Nsstring+regexcategory.h" @implementation nsstring (regexcategory) #pragma mark-Regular related-(BOOL) Isvalidatebyrege
  X: (NSString *) regex{nspredicate *pre = [nspredicate predicatewithformat:@ "SELF matches%@", regex];
return [pre evaluatewithobject:self]; #pragma mark-//Mobile phone Number service provider-(BOOL) ismobilenumberclassification{/** * Mobile number * Mobile: 134[0-8],135,136,137,138,139,15
  0,151,157,158,159,182,187,188,1705 * Unicom: 130,131,132,152,155,156,185,186,1709 * Telecom: 133,1349,153,180,189,1700 * *
   NSString * mobile = @ "^1 ((3//d|5[0-35-9]|8[025-9))//d|70[059]) \\d{7}$"//Total conditions/** 10 * China Mobile * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705/NSString * CM = @ "^1 (34[0-8]| (
  3[5-9]|5[017-9]|8[278]) \\d|705) \\d{7}$ "; /** 15 * China Unicom: Unicom * 130,131,132,152,155,156,185,186,1709/NSString * CU = @ "^1 (3[
  0-2]|5[256]|8[56]) \\d|709) \\d{7}$ "; /** 20 * China Telecom: Telecom 21 *133,1349,153,180,189,1700 * * NSString * CT = @ "^1 ((33|53|8[09)) \\d|349|700) \\d{7}$"; /** 25 * Mainland Fixed and PHS 26 * Area code: 010,020,021,022,023,024,025,027,028,029 27 * Number: seven-or eight-bit/NSST
  
  
  Ring * 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; }//Mobile number validity-(BOOL) ismobilenumber{/** * Mobile phone number 13, 15, 18, 170, 8 \d digit * PHS code: 010,020,021,022,023,024,025,027 , 028,029 also has not set the new Area code XXX/NSString *mobilenoregex = @ "^1 ((3\\d|5[0-35-9]|8[025-9)) \\d|70[059]) \\d{7}$";//all single-digit integers except 4,
  Cannot match with [^4,\\d], is this an 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); }//Mailbox-(BOOL) isemailaddress{nsstring *emailregex = @ "[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\\.[
  a-za-z]{2,4} ";
return [self Isvalidatebyregex:emailregex]; }//ID number-(BOOL) simpleverifyidentitycardnum {nsstring *regex2 = @ "^ (\\d{14}|\\d{17}) (\\d|[
  XX]) $ ";
return [self isvalidatebyregex:regex2]; //License plate-(BOOL) iscarnumber{//car number: Xiang k-de829 Hong Kong license plate: Guangdong z-j499 port NSString *carregex = @ "^[\u4e00-\u9fff]{1}[a-za-z]{1}[-][a
  -za-z_0-9]{4}[a-za-z_0-9_\u4e00-\u9fff]$ "//where \U4E00-\U9FA5 represents the encoded part of a Unicode encoding, \U9FA5-\U9FFF is a reserved part and may be added in the future
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) cont
Ainchinese 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}", A, Hanzi, (int) (MINLENTH-1), (int) (
  MAXLENTH-1)];
return [self Isvalidatebyregex:regex]; }-(BOOL) Isvalidwithminlenth: (Nsinteger) minlenth maxlenth: (nsinteger) Maxlenth Containchinese: (BOOL) cont Ainchinese 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%@]+)", I, Hanzi, Containothercharacter?
  Containothercharacter: @ ""];
  NSString *regex = [NSString stringwithformat:@ "%@%@%@%@", Lengthregex, Digtalregex, Letterregex, CharacterRegex];
return [self Isvalidatebyregex:regex]; #pragma mark-Algorithm related//exact identification number validity detection + (BOOL) Accurateverifyidcardnumber: (NSString *) value {value = [value Stringbytri
  
  Mmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]]; int length =0;
  if (!value) {return NO;
    
    }else {length = (int) value.length;
    if (length!=15 && length!=18) {return NO; }//Province code Nsarray *areasarray =@[@ "11", @ "12", @ "13", @ "14", @ "15", @ "21", @ "22", @ "23", @ "31", @ "32", @ "33", @ "34", @ "3" 5 ", @" 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];//test the legality of the date of birth}else {regularexpression = [[Nsregularexpression alloc]initwithpa ttern:@ "^[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:nsregularexpressioncaseinsensiti ve error:nil];//test the legality of the date of birth} Numberofmatch = [RegularExpression NUMBEROFM Atchesinstring:value Options:nsmatchingreportprogress ran
      
      Ge: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|0 7|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];//test Birth date legality}else {regularexpression = [[Nsregularexpression alloc] In itwithpattern:@ "^[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:nsregularexpressioncasei Nsensitive error:nil];//Test the legality of the date of birth} Numberofmatch = [regularexpression
           Numberofmatchesinstring:value                     Options:nsmatchingreportprogress range:nsmakerange (0, Value.length)]
      
      ; if (Numberofmatch >0) {int S = ([Value substringwithrange:nsmakerange (0,1)].intvalue + [value Substringwithrang E: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)];//judge Check bit if ([M Isequaltostring:[value Substringwithrange:nsmakerang
        E (17,1)]] {return yes;//check digit of check ID}else {return NO;
      }}else {return NO;
  } Default:return NO;
 }/** Bank card number validity problem Luhn algorithm * The current 16-digit UnionPay card number 6 is the beginning of the 622126~622925, 7 to 15 is the bank's custom, * may be the card-issuing branch, card-issuing outlets, card number, 16th is the parity code. * 16-bit card number check digit is calculated by Luhm check method: * 1, the 15-bit card number without parity digit is numbered 1 to 15 from right sequentially, the number on the odd digit is multiplied by 2 * 2, adding all 10 bits of the singularity product, plus the number * 3 on all even digits, adding and verifying the potential energy
 Divisible by 10. */-(BOOL) bankcardluhmcheck{NSString * Lastnum = [[Self substringfromindex: (self.length-1)] copy];//take out the last NSString * Forwardnum = [[Self substringtoindex: (self.length-1)] copy];//ago15 or 18 bits 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--) {//First 15-bit or the first 18-bit reverse-deposit array [Forwarddescarr Addobject:forwardarr[i]]; } Nsmutablearray * Arroddnum = [[Nsmutablearray alloc] initwithcapacity:0];//< 9 *2 of odd digits Nsmutablearray * arrOd dNum2 = [[Nsmutablearray alloc] initwithcapacity:0];//the product > 9 *2 of odd digits nsmutablearray = [[Arrevennum nsmutablearray C] initwithcapacity:0];//-bit array for (int i=0; i< forwarddescarr.count; i++) {Nsinteger num = [forwarddescarr[i
    ] Intvalue];
    if (i%2) {//even digit [Arrevennum addobject:[nsnumber Numberwithinteger:num]]; }else{//odd digits 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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.