iOS开发中各种正则匹配
程序员文章站
2022-07-14 19:29:49
...
正则匹配
做项目的时候避免不了要用各种的正则匹配,抽空整理了一下,话不多说了,直接上代码
- 正则匹配手机号
1. (BOOL)checkTelNumber:(NSString *) telNumber
{
NSString *pattern = @"^1+[0123456789]+\\d{9}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:telNumber];
return isMatch;
}
- 正则匹配手机号/电话
1. (BOOL)checkPhoneNumber:(NSString *) telNumber
{
NSString *pattern = @"^[1-9]+[0123456789]+\\d{9}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:telNumber];
return isMatch;
}
- 正则匹配邮箱
1. (BOOL) checkEmail:(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];
return [emailTest evaluateWithObject:email];
}
- 正则匹配用户密码6-16位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password
{
NSString *pattern = @"^[0-9a-zA-Z_#@]{6,16}";
// NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,16}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:password];
return isMatch;
}
- 正则匹配验证码
+ (BOOL)checkVerCode:(NSString *) verCode
{
NSString *pattern = @"^\\d{4,6}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:verCode];
return isMatch;
}
- 正则匹配用户姓名,20位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName
{
NSString *pattern = @"^[0-9a-zA-Z\u4E00-\u9FA5]{1,20}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:userName];
return isMatch;
}
- 正则匹配标签:中文或英文
+ (BOOL)checkTag : (NSString *) tag
{
NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,10}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:tag];
return isMatch;
}
- 正则匹配用户身份证号15或18位
+ (BOOL)checkUserIdCard: (NSString *) idCard
{
NSString *pattern = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:idCard];
return isMatch;
}
- 正则匹员工号,12位的数字
+ (BOOL)checkEmployeeNumber : (NSString *) number
{
NSString *pattern = @"^[0-9]{12}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:number];
return isMatch;
}
- 正则匹配URL
+ (BOOL)checkURL : (NSString *) url
{
NSString *pattern = @"^[0-9A-Za-z]{1,50}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:url];
return isMatch;
}
上一篇: python基础day4
下一篇: 正则表达式-匹配中英文、字母和数字
推荐阅读
-
使用设计模式中的Singleton单例模式来开发iOS应用程序
-
iOS应用开发中使用设计模式中的观察者模式的实例
-
iOS应用开发中UITableView的分割线的一些设置技巧
-
详解iOS App开发中UIViewController的loadView方法使用
-
iOS应用开发中UITabBarController标签栏控制器使用进阶
-
Android开发中TextView各种常见使用方法小结
-
iOS的UI开发中Modal的使用与主流应用UI结构介绍
-
详解iOS开发中app的归档以及偏好设置的存储方式
-
iOS开发中实现新闻图片的无限循环展示的方法
-
iOS开发中UITableview控件的基本使用及性能优化方法