Summary of Regular Expressions for iOS Development Verification Judgment Statements

  • 2021-08-17 01:14:13
  • OfStack

Preface

Everyone knows that the development of 1 can not be separated from some common verification formats, such as email, mobile phone number, etc. In the development process, 1 will generally build a new tool class to manage these verification methods and simplify the development process. 1 are generally used in the form of regular expressions to make judgments, this article enumerates a number of very practical items of a number of regular expression judgments, after the development of direct copy and paste can be, so greatly save the development time, the following do not say much, directly on the code.

1. Verify the mobile phone number:


+ (BOOL)isMobile:(NSString*)mobile
{
  NSString *regex = @"^1+[34578]+\d{9}";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  return [pred evaluateWithObject:mobile];
}

2. Mailbox authentication:


+ (BOOL)isEmail:(NSString*)email
{
  NSString *regex = @"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  return [pred evaluateWithObject:email];
}

3. Plastic verification


+ (BOOL)isPureInt:(NSString*)string{
  NSScanner* scan = [NSScanner scannerWithString:string];
  int val;
  return[scan scanInt:&val] && [scan isAtEnd];
}

4. Floating-point validation


- (BOOL)isPureFloat:(NSString*)string{
  NSScanner* scan = [NSScanner scannerWithString:string];
  float val;
  return[scan scanFloat:&val] && [scan isAtEnd];
}

5. Chinese Verification


+ (BOOL)isChinese:(NSString*)chinese
{
  NSString *regex = @"^[\u4e00-\u9fa5]";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  return [pred evaluateWithObject: chinese];
}

6. Website URL authentication


+ (BOOL)isURL:(NSString*)url
{
  NSString *regex = @"[a-zA-z]+://[^\s]*";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  return [pred evaluateWithObject: url];
}

7. ID number verification (cumbersome)


+ (BOOL)verifyIDCardNumber:(NSString *)IDCardNumber 
{ 
IDCardNumber = [IDCardNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
if ([IDCardNumber length] != 18) 
{ 
return NO; 
} 
NSString *mmdd = @ " (((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))) " ; 
NSString *leapMmdd = @ " 0229 " ; 
NSString *year = @ " (19|20)[0-9]{2} " ; 
NSString *leapYear = @ " (19|20)(0[48]|[2468][048]|[13579][26]) " ; 
NSString *yearMmdd = [NSString stringWithFormat:@ " %@%@ " , year, mmdd]; 
NSString *leapyearMmdd = [NSString stringWithFormat:@ " %@%@ " , leapYear, leapMmdd]; 
NSString *yyyyMmdd = [NSString stringWithFormat:@ " ((%@)|(%@)|(%@)) " , yearMmdd, leapyearMmdd, @ " 20000229 " ]; 
NSString *area = @ " (1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4} " ; 
NSString *regex = [NSString stringWithFormat:@ " %@%@%@ " , area, yyyyMmdd , @ " [0-9]{3}[0-9Xx] " ];

NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if (![regexTest evaluateWithObject:IDCardNumber])
{
  return NO;
}
int summary = ([IDCardNumber substringWithRange:NSMakeRange(0,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(10,1)].intValue) *7
+ ([IDCardNumber substringWithRange:NSMakeRange(1,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(11,1)].intValue) *9
+ ([IDCardNumber substringWithRange:NSMakeRange(2,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(12,1)].intValue) *10
+ ([IDCardNumber substringWithRange:NSMakeRange(3,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(13,1)].intValue) *5
+ ([IDCardNumber substringWithRange:NSMakeRange(4,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(14,1)].intValue) *8
+ ([IDCardNumber substringWithRange:NSMakeRange(5,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(15,1)].intValue) *4
+ ([IDCardNumber substringWithRange:NSMakeRange(6,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(16,1)].intValue) *2
+ [IDCardNumber substringWithRange:NSMakeRange(7,1)].intValue *1 + [IDCardNumber substringWithRange:NSMakeRange(8,1)].intValue *6
+ [IDCardNumber substringWithRange:NSMakeRange(9,1)].intValue *3;
NSInteger remainder = summary % 11;
NSString *checkBit = @"";
NSString *checkString = @"10X98765432";
checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];//  Judge parity bit 
return [checkBit isEqualToString:[[IDCardNumber substringWithRange:NSMakeRange(17,1)] uppercaseString]];
}

8. 1 Authentication of Generic User Names


+ (BOOL) isUserName:(NSString *)name
{
  NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
  NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex] ;   
  return [userNamePredicate evaluateWithObject:name];
}

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: