#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; }