废话不多说了,直接给大家贴实现此功能的正则表达式代码了,具体代码如下所示:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#import <Foundation/Foundation.h>
int main() {
// ? == {0,1}
// * == {0,无穷}
// + == {1,无穷}
// \\d == [0-9]
// \\w == [A-Za-z_0-9]
// * 的意思是可有可无
// [a|b|c]+ 表示三个至少出现一次或多次
//检测电话号码是否正确
NSString *tel = @ "" ;
//正则表达式
NSString *regex = @ "^\\\\d*$" ;
// NSString *regex = @"^[0-9]{3,4}-[0-9]{7,8}$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@ "SELF MATCHES%@" ,regex]; //创建需要满足上面的正则表达式的谓词
NSLog(@ "该电话号码:%d" ,[predicate evaluateWithObject:tel]);
//用户名 (第一位必须是字母,6-16位,只能有字母,数字或下划线)
NSString *user = @ "m54355" ;
NSString *regex1 = @ "^[A-Za-z]\\\\w{5,15}$" ;
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@ "SELF MATCHES%@" ,regex1];
// NSLog(@"该电话号码:%d",[predicate1 evaluateWithObject:user]);
// //身份证
// NSString *user1 = @"610125199301300814";
// NSString *regex2 = @"^\\\\d{17}[\\\\dxX]$";
// NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",regex2];
// NSLog(@"该身份证:%d",[predicate2 evaluateWithObject:user1]);
//邮箱
NSString *mailbox = @ "101707383@qq.com" ;
NSString *regex3 = @ "^[a-zA-Z0-9._%+-]+@[A-Za-z0-9.-]+\\\\.[A-Za-z]{2,4}$" ;
NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@ "SELF MATCHES%@" ,regex3];
// NSLog(@"该邮箱:%d",[predicate3 evaluateWithObject:mailbox]);
//手机号
// NSString *phone = @"18709259205";
// NSString *regex4 = @"^1[3|4|5|7|8]\\\\d{9}$";
// NSPredicate *predicate4 = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",regex4];
// NSLog(@"该手机:%d",[predicate4 evaluateWithObject:phone]);
if ([predicate1 evaluateWithObject:user] == 1) {
if ([predicate3 evaluateWithObject:mailbox] == 1) {
NSLog(@ "登录成功" );
}
} else {
NSLog(@ "错误" );
}
return 0;
}
|
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 94
-
2025-06-04 49
-
2025-05-27 76
-
2025-06-04 43
-
nginx与apache限制ip并发访问 限制ip连接的设置方法
2025-05-26 79
热门评论