废话不多说了,直接给大家贴实现此功能的正则表达式代码了,具体代码如下所示:
#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; }
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论