IOS regular expressions determine phone numbers, mailboxes, etc.

Source: Internet
Author: User

  1 #import "Nsstring+regexcategory.h" 2 3 @implementation NSString (regexcategory) 4 #pragma mark-Regular related 5-(BOOL) I  Svalidatebyregex: (NSString *) regex{6 nspredicate *pre = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex];  7 return [pre evaluatewithobject:self]; 8} 9 #pragma mark-11 12//Mobile phone Number service provider-(BOOL) ismobilenumberclassification{14/** 15 * Mobile number 16 * Mobile      : 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705 17 * Unicom: 130,131,132,152,155,156,185,186,1709 18 * Telecom: 133,1349,153,180,189,1700 *//NSString * MOBILE = @ "^1 ((3//d|5[0-35-9]|8[025-9])//d|70[059]) \ \d{7}$ ";//General Conditions 21 22/** 23 10 * Mobile: China Mobile 24 11 * 134[0-8],135,136,137,138,139,1 50,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}$ "; 27/** 28 15 * Unicom: China Unicom 29 16 * 130,131,132,152,155,156,185,186,1709 * * * NSString * CU = @ "^1 ((3[0-2]|5[256]|8[56]) \\d|709) \\d{7}$";      32/** 33 20 * China Telecom: Chinese Telecom 34 21 * 133,1349,153,180,189,1700 35 22 * 36 NSString * CT = @ "^1 ((33|53|8[09)) \\d|349|700) \\d{7}$";  37 38 39/** 40 25 * Mainland China fixed and PHS 41 26 * Area code: 010,020,021,022,023,024,025,027,028,029 42 27 * Number: seven-bit or eight-bit * * NSString * PHS = @ "^0 (10|2[0-5789]|\\d{3}) \\d{7,8}$"; Nspredicate *regextestmobile = [Nspredicate predicatewithformat:@ "Self MATCHES%@", MOBILE]; if ([self isvalidatebyregex:cm]) 50 | | ([self ISVALIDATEBYREGEX:CU]) 51 | | ([self isvalidatebyregex:ct]) 52 | | ([self isvalidatebyregex:phs])) +------------------ OL) ismobilenumber{64/** 65 * Mobile phone numberStart with 13, 15, 18, 170, 8 \d Numeric characters 66 * PHS Area code: 010,020,021,022,023,024,025,027,028,029 There is a new area code that is not set XXX * * * Nsstri ng *mobilenoregex = @ "^1 ((3\\d|5[0-35-9]|8[025-9)) \\d|70[059]) \\d{7}$";////except for all single-digit integers other than 4, cannot use [^4,\\d] match, is this an iOS Bug? NSString *phsregex [email protected] "^0 (10|2[0-57-9]|\\d{3}) \\d{7,8}$"; (+) BOOL ret = [self isvalidatebyregex:mobilenoregex]; Ret1 BOOL = [self Isvalidatebyregex:phsregex]; Return (ret | | ret1); 75} 76 77//Email-(BOOL) isemailaddress{nsstring *emailregex = @ "[a-za-z0-9._%+-][email protected][a-za-z0 -9.-]+\\. [A-za-z] {2,4} "; return [self Isvalidatebyregex:emailregex]; 81} 82 83//Social Security number-(BOOL) simpleverifyidentitycardnum-NSString *regex2 = @ "^ (\\d{14}|\\d{17}) (\\d|[ XX]) $ "; Isvalidatebyregex:regex2 return [self]; 88} 89 90//License plate-(BOOL) iscarnumber{92//Vehicle grade: Xiang k-de829 Hong Kong license number: Yue z-j499 port NSString *carregex = @ "^[\u4e00-\u9f ff]{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 the Chinese character in Unicode encoding, \U9FA5-\U9FFF is a reserved part and may be added in the future 94 return [self Isvalidatebyregex:carregex]; -(BOOL) ismacaddress{98 NSString * Macaddregex = @ "([a-fa-f\\d]{2}:) {5}[a-fa-f\\d]{2}"; return [S] Elf isvalidatebyregex:macaddregex];100}101 102-(BOOL) isValidUrl103 {104 NSString *regex = @ "^ ((http) | ( HTTPS)) +:[^\\s]+\\. [^\\s]*$]; return [self isvalidatebyregex:regex];106}107 108-(BOOL) isvalidchinese;109 {nsstring *chinese Regex = @ "^[\u4e00-\u9fa5]+$"; 111 return [Self isvalidatebyregex:chineseregex];112}113-(BOOL) Isvalidpostalcode {NSString *postalregex = @ "^[0-8]\\d{5} (?! \\d) $ ";" return [self isvalidatebyregex:postalregex];117}118 119-(BOOL) isValidTaxNo120 {121 NSString *taxnore Gex = @ "[0-9]\\d{13} ([0-9]|                    X) $ "; 122 return [Self isvalidatebyregex:taxnoregex];123}124-(BOOL) Isvalidwithminlenth: (Nsinteger) minLenth126 Maxlenth: (Nsinteger) MaxLenth127 Containchinese: (bool) containChinese128 firstcannotbedigtal: (bool) firstcannotbedigtal;129 {130 [\u4e00-\u9fa5a-za-z0-9_]{4,20}131 nsstring *hanzi = Containchinese? @ "\u4e00-\u9fa5": @ "", nsstring *first = firstcannotbedigtal?  @ "^[a-za-z_]": @ ""; 133 134 NSString *regex = [NSString stringwithformat:@ "%@[%@a-za-z0-9_]{%d,%d}", First, Hanzi, (int) (minLenth-1), (int) (MAXLENTH-1)];135 return [self isvalidatebyregex:regex];136}137 138-(BOOL) Isvalidwithminlenth: (N Sinteger) minLenth139 Maxlenth: (nsinteger) maxLenth140 Containchinese: (BOOL) ContainChinese14 1 containdigtal: (bool) containDigtal142 Containletter: (bool) containLetter143 Containother  Character: (NSString *) containOtherCharacter144 firstcannotbedigtal: (BOOL) firstcannotbedigtal;145 {146 NSString *hanzi = Containchinese? @ "\u4e00-\u9fa5": @ ""; 147 nsstring *first = firstcannotbedigtal? @ "^[a-za-z_] ": @" "; 148 nsstring *lengthregex = [NSString stringwithformat:@" (? =^.{ %@,%@}$) "@ (Minlenth), @ (maxlenth)];149 nsstring *digtalregex = containdigtal? @ "(? = (. *\\d.*) {1})": @ ""; "nsstring *letterregex = containletter? @ "(? = (. *[a-za-z].*) {1})": @ ""; 151 nsstring *characterregex = [NSString stringwithformat:@ "(?:%@[%@a-za-z0-9%@]+)", fi RST, Hanzi, Containothercharacter? Containothercharacter: @ ""];152 nsstring *regex = [NSString stringwithformat:@ "%@%@%@%@", Lengthregex, DigtalRegex, L Etterregex, characterregex];153 return [self isvalidatebyregex:regex];154}155 156 #pragma mark-algorithm-related 157//exact ID number valid Sex detection 158 + (BOOL) Accurateverifyidcardnumber: (NSString *) value {159 value = [Value Stringbytrimmingcharactersinset:[nscha     Racterset whitespaceandnewlinecharacterset]];160 161 int Length =0;162 if (!value) {163 return no;164 }else {165 length = (int) value.length;166 167 if (length!=15 && length!=18) {168             return no;169}170}171//Province Code 172 nsarray *areasarray [email protected][@ "11", @ "12" , @ "13", @ "14", @ "15", @ "21", @ "22", @ "23", @ "31", @ "32", @ "33", @ "34", @ "35", @ "36", @ "37", @ "41", @ "42", @ "43", @ "44", @ "45" @ "The", @ "", @ "," @ "," @ "", @ "", @ "", @ "", @ "", @ "", @ "", @ "", @ "", @ "", @ "", "@" "", @ "" ""         *valuestart2 = [value substringtoindex:2];175 BOOL areaflag =no;176 for (NSString *areacode in Areasarray) {177     if ([AreaCode Isequaltostring:valuestart2]) {178 Areaflag =yes;179 break;180}181 }182 183 if (!areaflag) {184 return false;185}186 187 188 Nsregularexpression *regulare             xpression;189 nsuinteger numberofmatch;190 191 int Year =0;192 switch (length) {193 Case 15:194 Year = [value Substringwithrange:nsmakerange (6,2)].intvalue +1900;195 196 if (Year%4 = = 0 | | (Year%100 ==0 &&Amp Year%4 ==0) {197 198 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}$ "199 options:nsregularexpress ionCaseInsensitive200 error:nil];//Test the legality of the date of birth 20 1}else {202 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}$] 203 o ptions:nsregularexpressioncaseinsensitive204 Err or:nil];//test the legality of birth date 205}206 numberofMatch = [RegularExpression numberofmatchesinstring:value207  options:nsmatchingreportprogress208 Range:nsmakerange (0, Value.length)];209 if (Numberofmatch >0) {211 return yes;212}e LSE {213 Return no;214}215 case 18:216 year = [value substringwithrange:n Smakerange (6,4)].intvalue;217 if (year%4 ==0 | | (Year%100 ==0 && year%4 ==0)) {218 219 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:nsregular                                ExpressionCaseInsensitive221                                            error:nil];//test date of birth legality 222}else {223 Regularexpre Ssion = [[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]$ "224                                                                            options:nsregularexpressioncaseinsensitive225 error:nil];//test date of birth legality 226}227 numberofmatch = [RegularExpression numberofmatchesinstring:v                                                                  alue228 options:nsmatchingreportprogress229 Range:nsmakerange (0, value.length)];230 231 if (n Umberofmatch >0) {232 int S = ([Value substringwithrange:nsmakerange (0,1)].intvalue + [value S]Ubstringwithrange:nsmakerange (10,1)].intvalue) *7 + ([value substringwithrange:nsmakerange]].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) + ([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) + ([value Substringwithrange:nsmakerange (6,1)].intValue + [value Substringwithrange:nsmakerange (16,1)].intvalue) * + [value substringwithrange:nsmakerange (7,1)].intValue * + [value]                 Substringwithrange:nsmakerange (8,1)].intvalue *6 + [value substringwithrange:nsmakerange (9,1)].intValue *3;233 int Y = S%11;234 nsstring *m [email pRotected] "F"; 235 nsstring *jym [email protected] "10x98765432"; 236 M = [Jym substringw Ithrange:nsmakerange (y,1)];//judgment Check bit 237 if ([M isequaltostring:[value Substringwithrange:nsmakerange (17,1)]                 ] {238 return yes;//check digit for detection ID 239}else {return no;241 }242 243}else {244 return no;245}246 default:24  7 return no;248}249}250 251 252 253/** Bank card number validity problem Luhn algorithm 254 * Current 16-bit UnionPay card current card number beginning 6 bit is 622126~622925, 7 To 15 bits is the bank custom, 255 * may be the card issuing branch, the card issuing network, the card issuing serial number, 16th digit is the check code. 256 * 16-bit card number check digit is calculated using the Luhm check method: 257 * 1, the 15-bit card number without the check digit is numbered 1 to 15 from the right, the number on the odd number is multiplied by 2258 * 2, all the 10 bits of the singular product are added together, plus the number 25 on all even digits 9 * 3, the addition and calibration potentials are divisible by 10. 260 */261-(BOOL) bankcardluhmcheck{262 NSString * Lastnum = [self Substringfromindex: (self.length-1)] copy];//remove Last Bit 263 NSString * Forwardnum = [[Self Substringtoindex: (self.lengtH-1)] copy];//first 15 or 18 265 nsmutablearray * Forwardarr = [[Nsmutablearray alloc] initwithcapacity:0];266 fo R (int i=0; i<forwardnum.length; i++) {267 NSString * subStr = [Forwardnum substringwithrange:nsmakerange (i, 1) ];268 [Forwardarr addobject:substr];269}270 271 nsmutablearray * Forwarddescarr = [[Nsmutablearray al LOC] initwithcapacity:0];272 for (int i = (int) (forwardarr.count-1), i>-1; i--) {///first 15-bit or first 18-bit reversed-deposit in array 273 [fo Rwarddescarr addobject:forwardarr[i]];274}275 276 nsmutablearray * Arroddnum = [[Nsmutablearray alloc] InitWi thcapacity:0];//odd number of the product < 9277 Nsmutablearray * arrOddNum2 = [[Nsmutablearray alloc] initwithcapacity:0];//odd digit > 9278 Nsmutablearray * arrevennum = [[Nsmutablearray alloc] initwithcapacity:0];//even digit array 279 280 for (int i= 0; i< Forwarddescarr.count; i++) {281 Nsinteger num = [Forwarddescarr[i] intvalue];282 if (i%2) {//even digit 283 [ArrevennUm addobject:[nsnumber numberwithinteger:num]];284}else{//Odd digit 285 if (NUM * 2 < 9) {286 [Arroddnum addobject:[nsnumber numberwithinteger:num * 2]];287}else{288 Nsinteger Decade num = (num * 2)/10;289 Nsinteger unitnum = (num * 2)% 10;290 [arrOddNum2 addobject:[nsn             Umber numberwithinteger:unitnum]];291 [arrOddNum2 addobject:[nsnumber numberwithinteger:decadenum]];292 }293}294}295 296 __block nsinteger sumoddnumtotal = 0;297 [Arroddnum enumerateobjects     usingblock:^ (NSNumber * obj, Nsuinteger idx, BOOL *stop) {298 Sumoddnumtotal + = [obj integervalue];299}];300 301 __block Nsinteger sumoddnum2total = 0;302 [arrOddNum2 enumerateobjectsusingblock:^ (NSNumber * obj, NSUInte Ger idx, BOOL *stop) {303 Sumoddnum2total + = [obj integervalue];304}];305 306 __block Nsinteger Sumev ennumtotal = 0; 307 [Arrevennum enumerateobjectsusingblock:^ (nsnumber * obj, Nsuinteger idx, BOOL *stop) {308 Sumevennumtotal + = [obj I ntegervalue];309}];310 311 Nsinteger lastnumber = [lastnum integervalue];312 313 Nsinteger luhmTotal = Lastnumber + sumevennumtotal + sumoddnum2total + sumoddnumtotal;314 315 return (luhmtotal%10 ==0)? yes:no;316}317 318-(BOOL) isipaddress{319 nsstring *regex = [NSString stringwithformat:@ "^ (\\d{1,3}) \ \. ( \\d{1,3}) \ \. (\\d{1,3}) \ \.  (\\d{1,3}) $ "];320 nspredicate *pre = [Nspredicate predicatewithformat:@" Self MATCHES%@ ", regex];321 BOOL rc = [Pre evaluatewithobject:self];322 323 if (RC) {324 Nsarray *componds = [self componentsseparatedbystring:@ ', '] ; 325 326 BOOL v = yes;327 for (nsstring *s in componds) {328 if (S.integervalue > 2 ) {329 v = no;330 break;331}332}333 334 return v;3 }336 337 REturn no;338}339 340 341 342 @end 

IOS Regular Expressions determine phone numbers, mailboxes, etc.

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.