Can write a nssring category to add some methods to NSString, and these methods are some regular expressions.
Like writing a class called Helper, it's nsstring+helper.
The. h and. m files are then written.
#pragma mark-Verify related/* Verify related *///1. Is the mobile number- (BOOL) ISMOBILENUMBER;//2. Is the mailbox-(BOOL) ISVALIDATEEMAIL;//3. Whether it is an empty string- (BOOL) ISNULLSTR;//4. Whether it is an empty object-(BOOL) isnull;//5. Numeric to String + (nsstring*) Stringfrominteger: (Nsinteger ) NUM;//6. Whether the password conforms to the combination of the standard 8-14-bit alphabetic array-(BOOL) isvalidatepwd; #pragma mark-function-related//function-related *///1.MD5 encryption-(NSString *) md5;// 2. Remove the end-to-end whitespace of the string-(NSString *) trim;
. m file
/* Verify the relevant *///1. Is the phone number-(BOOL) ismobilenumber{NSString * MOBILE = @ "^1[3-9]\\d{9}$"; Nspredicate *regextestmobile = [Nspredicate predicatewithformat:@ "Self MATCHES%@", MOBILE]; if ([regextestmobile evaluatewithobject:self] = =YES) {returnYES; }else{returnNO; }}//2. Whether it is a mailbox-(BOOL) isvalidateemail{nsstring *emailregex = @ "^ ([a-z0-9a-z]+[-_|\\.]?) +[a-z0-9a-z]@ ([a-z0-9a-z]+ (-[a-z0-9a-z]+) \ \.) +[a-za-z]{2,}$ "; Nspredicate *emailtest = [Nspredicate predicatewithformat:@ "Self MATCHES%@", Emailregex]; Return[Emailtest evaluatewithobject:self];} 3. Whether it is an empty string-(BOOL) isnullstr{if ([Self isequaltostring:@ <NULL> "]) {returnYES; }else if ([self isequaltostring:@ "(null)"]) {returnYES; }else if ([Self isequaltostring:@ "(NULL)"]) {returnYES; }else if ([Self isequaltostring:@ <null> "]) {returnYES; } returnNO; }//4. Whether it is an empty object-(BOOL) isnull{//Determine if empty string if ([Self isequal:[nsnull null]]) {returnYES; } else if ([Self Iskindofclass:[nsnull class]]) {returnYES; } else if (self==Nil) {returnYES; } returnNO; }//5. Numeric to String + (nsstring*) Stringfrominteger: (Nsinteger) num{return [NSString stringwithformat:@ "%ld", num];} 6. Whether the password conforms to the standard 8-14-character array combination-(BOOL) isvalidatepwd{nsstring *pwdregex = @ "^ (?! [0-9]+$) (?! [a-za-z]+$] [0-9a-za-z]{8,14}$]; Nspredicate *pwdtest = [Nspredicate predicatewithformat:@ "self MATCHES%@", Pwdregex]; return [Pwdtest Evaluatewithobject:self];} #pragma mark-Function-related/* function-related *///1.MD5 encryption-(NSString *) md5{const char* str = [self utf8string]; unsigned char r Esult[cc_md5_digest_length]; CC_MD5 (str, (Cc_long) strlen (str), result); nsmutablestring *ret = [nsmutablestring stringwithcapacity:cc_md5_digest_length*2]; for (int i = 0; i<CC_MD5_ Digest_length; i++) {[ret appendformat:@ "%02x", Result[i]];} return ret;} 2. Remove the end and end spaces of the string-(NSString *) trim{return [self stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]];}
That's it. When using the Import class name: #import "Nsstring+helper"
And then use it, you can use it directly.
Like what:
Verify the phone number nsstring *phonenum = @ "17778176004"; NSLog (@ "phonenum:%d", [Phonenum Ismobilenumber]); Email verification NSString *emailstr = @ "[email protected]"; NSLog (@ "emailstr:%d", [Emailstr isvalidateemail]); MD5 encryption This rule needs to set up before and after the unified nsstring *md5str = @ "ABCDEFG"; NSLog (@ "md5str:%@", [md5str MD5]);//Remove the space before and after nsstring *trimstr = @ "1234567890"; NSLog (@ "trimstr:%@", [trimstr trim]);//is an empty string nsstring *nullstr = @ "(null)"; NSLog (@ "nullstr:%d", [Nullstr isnullstr]);//Whether the empty object seems to be set to empty when it can not fall up the method nsstring *nullobj = nil; [Nullobj IsNull]; NSLog (@ "null:%d", [Nullobj isNull]);
If you encounter any problems in the use of the process, you can contact me QQ [email protected]
Grow together.
Regular expressions commonly used in iOS (mobile phone number mailbox MD5 encryption validation empty string, etc.)