博客> 常用的几种正则匹配
常用的几种正则匹配
2017-10-18 01:34 评论:0 阅读:256 qianqianzhang
ios 车牌 姓名 正则匹配(手机号 小数)

1.正则匹配手机号(只对合不合法匹配,不区分电信,移动,联通)

  • (BOOL)checkTelNumber:(NSString) telNumber { NSStringpattern =@"^1(3[0-9]|4[57]|5[0-9]|7[6-8]|8[0-9])\d{8}"; NSPredicate*pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern]; BOOL isMatch = [pred evaluateWithObject:telNumber];

    return isMatch; }

  1. 正则匹配验证码(数字字母组合)
    • (BOOL)checkSecurity:(NSString*) security

{ NSString*pattern =@"^[A-Za-z0-9]+$" ;

NSPredicate*pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
BOOL isMatch = [pred evaluateWithObject:security];

return isMatch;

}

  1. 正则匹配用户密码8-20位数字和字母组合
    • (BOOL)checkPassword:(NSString*) password

{ NSStringpattern =@"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{8,20}"; NSPredicatepred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern]; BOOL isMatch = [pred evaluateWithObject:password]; return isMatch; }

  1. 正则匹配用户姓名,10位的中文

    • (BOOL)checkUserName : (NSString ) userName { NSString pattern = @"^[一-龥]{1,10}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:userName]; return isMatch; }
  2. 正则匹配用户昵称,4-20位的中文或英文或数字或“-”“_”

    • (BOOL)checkUserNickName : (NSString ) userName { NSString pattern = @"^[0-9a-zA-Z一-龥_-]{4,20}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:userName]; return isMatch; }
  3. 正则匹配数字或小数

    • (BOOL)checkFigures:(NSString ) figures { NSString pattern = @"^([1-9][0-9]{0,6}|0)([.][0-9]+)?$"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:figures]; return isMatch; }
  4. 正则匹配17位数字

    • (BOOL)checkMoreFigures:(NSString ) number; { NSString pattern = @"^[0-9]{17}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:number]; return isMatch; }
  5. 正则匹配num位大写字母和数字的字符串

    • (BOOL)checkCarInfo:(NSString )carInfo WithNum:(NSUInteger)num { NSString pattern = [NSString stringWithFormat:@"^[A-Z0-9]{%lu}$",(unsigned long)num]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:carInfo]; return isMatch; }
  6. 正则匹配车轮number位参数是够为数字

    • (BOOL)checkWheelNumber:(NSString )wheelNumber withNumber:(NSInteger)number { NSString pattern = [NSString stringWithFormat:@"^[0-9]{%lu}$",(unsigned long)number]; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:wheelNumber]; return isMatch; }
  7. 正则匹配正整数或含有number位小数的字符串

    • (BOOL)checkPrice:(NSString )price withDecimalPoint:(NSInteger)number { NSString pattern = [NSString stringWithFormat:@"^[0-9]+(.[0-9]{%lu})?$",(unsigned long)number] ; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:price];

    return isMatch; }

收藏
0
sina weixin mail 回到顶部