Common Regular Expressions

Source: Internet
Author: User

/** * MOBILE Number Verification * * @param mobilenumbel Incoming phone number * * @return format correctly returns Yes error return NO */+ (BOOL) IsMobile: (NSString *) mobilenumb el{/** * Mobile phone number * Move: 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 * Unicom: 130,131,132,152,155,1 56,185,186 * Telecom: 133,1349,153,180,189,181 (added) * Added: 14_,17_ * */nsstring * MOBIL = @ "^1 (3[0-9]|4[0-9]|5[0-35-9    ]|7[0-9]|8[025-9]) \\d{8}$ "; /** 10 * Mobile: China Mobile 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 1 2 */nsstring * CM = @ "^1 (34[0-8]| (    3[5-9]|5[017-9]|8[2378] \\d) \\d{7}$ ";  /** 15 * Unicom: China Unicom * 130,131,132,152,155,156,185,186 * * * NSString * CU    = @ "^1 (3[0-2]|5[256]|8[56]) \\d{8}$";  /** 20 * China Telecom: Telecom 21 * 133,1349,153,180,189,181 (Increase) * * * NSString * CT =    @ "^1 ((33|53|8[019]) [0-9]|349) \\d{7}$"; Nspredicate *regextestmobile = [Nspredicate predicatewithformat:@]Self MATCHES%@ ", MOBIL];    Nspredicate *REGEXTESTCM = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CM];    Nspredicate *REGEXTESTCU = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CU];    Nspredicate *REGEXTESTCT = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CT]; if ([Regextestmobile Evaluatewithobject:mobilenumbel] | | [REGEXTESTCM Evaluatewithobject:mobilenumbel] | | [REGEXTESTCT Evaluatewithobject:mobilenumbel] | | [Regextestcu Evaluatewithobject:mobilenumbel]))    {return YES; } return NO; /** * Password Verification * * @param password incoming password 6-16-bit Case Number * * @return format correctly returns Yes error return NO */+ (BOOL) Ispassword: (NSString *) pass    word{nsstring *pwd = @ "^[a-za-z0-9]{6,16}$";    Nspredicate *regextestpwd = [Nspredicate predicatewithformat:@ "Self MATCHES%@", PWD];    if ([Regextestpwd Evaluatewithobject:password]) {return YES; } return NO; /** * Email Verification * * @param email Incoming mailbox * * @return format correctly returns Yes error return NO */+ (BOOL) Isemail:(NSString *) email{nsstring *emailregex = @ "[a-z0-9a-z._%+-][email protected][a-za-z0-9.-]+\\.[    a-za-z]{2,4} ";    Nspredicate *emailtest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Emailregex];    if ([Emailtest Evaluatewithobject:email]) {return YES; } return NO; /** * ID Number Verification * * @param identitycard Incoming ID number * * @return format correctly returns Yes error return NO */+ (BOOL) Isidentitycard: (NSString *) IDE ntitycard{nsstring *idnum = @ "^ (\\d{14}|\\d{17}) (\\d|[    XX]) $ ";    Nspredicate *identitycardpredicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Idnum]; return [Identitycardpredicate Evaluatewithobject:identitycard];} /** Bank card number validity problem Luhn algorithm * Current 16-bit UnionPay card current card number at the beginning of 6 is between 622126~622925, 7 to 15 bits is the bank custom, * may be the card issuing branch, card issuing outlets, issuing serial number, 16th digit is the check code. * The 16-bit card number check digit is calculated using the Luhm check method: * 1, the 15-bit card number without the check digit is numbered from right to 1 to 15, the number on the odd number is multiplied by 2 * 2, all the 10 bits of the singular product are added together, plus the number on all even digits * 3, adding and adding check digit Can be divisible by 10. */+ (BOOL) Isbankcardluhmcheck: (NSString *) banknumber{NSString * Lastnum = [[Banknumber SUBSTRINGFROmindex: (banknumber.length-1)] copy];//take out the last NSString * forwardnum = [[Banknumber Substringtoindex: (    BANKNUMBER.LENGTH-1)] copy];//first 15 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 first 18-bit reverse-deposit array [Forwarddescarr Addobject:forwardarr[i]]    ; } Nsmutablearray * Arroddnum = [[Nsmutablearray alloc] initwithcapacity:0];//odd digit < 9 Nsmutablearray * ARRODDN UM2 = [[Nsmutablearray alloc] initwithcapacity:0];//odd digit > 9 Nsmutablearray * arrevennum = [[Nsmutablearray alloc ] initwithcapacity:0];//even-bit array for (int i=0; i< forwarddescarr.count; i++) {Nsinteger num = [forwarddescarr[i        ] Intvalue]; if (i%2) {//even digits [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:^ (id _nonnull obj, nsuinteger idx, BOOL * _nonnull stop)    {sumoddnumtotal + = [obj IntegerValue];    }];    __block Nsinteger sumoddnum2total = 0; [ArrOddNum2 enumerateobjectsusingblock:^ (id _nonnull obj, nsuinteger idx, BOOL * _nonnull stop)    {sumoddnum2total + = [obj IntegerValue];    }];    __block Nsinteger sumevennumtotal = 0; [Arrevennum enumerateobjectsusingblock:^ (id _nonnull obj, NSuinteger idx, BOOL * _nonnull stop) {sumevennumtotal + = [obj IntegerValue];    }];    Nsinteger lastnumber = [Lastnum integervalue];    Nsinteger luhmtotal = lastnumber + sumevennumtotal + sumoddnum2total + sumoddnumtotal; Return (luhmtotal%10 ==0)? Yes:no;} @end

Common regular Expressions

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.