正則表達式之用戶名、密碼、手機號碼、郵箱、地址的字符序列規則

正則表達式之用戶名的字符序列規則

用戶名開頭不能是數字,不能包含空格,特殊標點符號,長度在 2 到 12 之間:

第一種寫法:String user = "[a-zA-Z\u4E00-\u9FA5]{1}[a-zA-Z0-9\u4E00-\u9FA5]{1,12}";

第二種寫法:String user = "[^\\d^\\s^\\p{Punct}[\u4E00-\u9FA5]]{1}[^\\s^\\p{Punct}[\u4E00-\u9FA5]]{1,12}";

第三種寫法:

String user = "[^\\p{Blank}^\\p{Digit}^\\p{Punct}[\u4E00-\u9FA5]]{1}[^\\p{Blank}^\\p{Punct}[\u4E00-\u9FA5]]{1,12}";


正則表達式之密碼的字符序列規則

密碼開頭第一位必須是大寫字母,不能包含空格,特殊標點符號,漢字,長度在 6 到 12 之間:

第一種寫法:String password = "[A-Z]{1}[a-zA-Z0-9]{5,15}";

第二種寫法:String password = "\\p{Upper}{1}[\\p{Alpha}\\d]{5,15}";

第三種寫法:String password = "[\\p{Alpha}&&[A-Z]]{1}[\\p{Digit}[a-zA-Z]]{5,15}";


正則表達式之手機號碼的字符序列規則

手機號碼只能是數字、長度在5到11之間:

第一種寫法:String number = "[\\d]{5,11}";

第二種寫法:String number = "[0-9]{5,11}";

第三種寫法:String number = "\\p{Digit}{5,11}";


正則表達式之郵箱的字符序列規則(已QQ郵箱格式爲例)

QQ郵箱只能是數字開頭、後綴必須是@qq.com、長度在5到11之間:

第一種寫法:String email = "[\\d]{5,11}@qq.com";  
第二種寫法:String email = "[0-9]{5,11}@qq.com";  

第三種寫法:String email = "\\p{Digit}{5,11}@qq.com";  


正則表達式之地址的字符序列規則

地址可以包含漢字,數字,字母,不能包含空格、特殊標點符號,長度在2-26之間;

第一種寫法:String address = "[a-zA-Z0-9\u4E00-\u9FA5]{2,26}";
第二種寫法:String address = "[\\p{Alpha}[\\d][\u4E00-\u9FA5]]{2,26}";

第三種寫法:String address = "[\\p{Lower}\\p{Upper}\\p{Digit}[\u4E00-\u9FA5]]{2,26}";


通過以上這些規則,編寫了一個Java簡單的用戶註冊流程

代碼如下↗:

import java.util.Scanner;
public class Regular_ExproessionTest {

	//創建用戶名規則:數字不能開頭、不能包含空格,特殊標點字符、長度在2到12之間;
	static String user = "[a-zA-Z\u4E00-\u9FA5]{1}[a-zA-Z0-9\u4E00-\u9FA5]{1,12}"; 

	//創建密碼規則:第一個必須是大寫字母、不能包含空格,特殊標點字符,漢字、長度在6到16之間;
	static String password = "[A-Z]{1}[a-zA-Z0-9]{5,15}";   //第一種寫法

	//創建手機號碼規則:只能是數字、長度在5到11之間
	static String number = "[\\d]{5,11}";   //第一種寫法

	//創建郵箱規則(QQ郵箱爲例:QQ號碼@qq.com),後綴必須是@qq.com,QQ號碼長度在5到11之間
	static String email = "[\\d]{5,11}@qq.com";   //第一種寫法

	//創建地址規則,不能包含空格、特殊標點符號,長度在2-24之間;
	static String address = "[a-zA-Z0-9\u4E00-\u9FA5]{2,24}";  //第一種寫法

	public static void main(String[] args) {
		
		//創建一個String類型數組保存註冊信息
		String [] Messger = new String[5];
		
		System.out.println("*****歡迎光臨註冊界面*****");
		Scanner sc = new Scanner(System.in);
		
		if(true){
			System.out.println("請輸入以下注冊信息:");
System.out.println("用戶名:(數字不能開頭、不能包含空格,特殊標點字符、長度在2到12之間)");
			
			//註冊用戶名
			for(int i=5;i>0;i--){
				sc = new Scanner(System.in);
				String use = sc.nextLine();
				
				if(use.matches(user)){
					Messger[0] = use;
System.out.println("正確,請輸入密碼:(第一個必須是大寫字母、不能包含空格,特殊標點字符,漢字、長度在6到16之間)");
					break;
				}else{
					System.out.println("錯誤,請重新輸入,還有:"+i+" 次輸出機會");
				}
			}
			
			if(Messger[0] == null){
				System.out.println("錯誤,用戶名信息未註冊完成");
			}else{
				//註冊密碼
				for(int i=5;i>0;i--){
					sc = new Scanner(System.in);
					String use = sc.nextLine();
					
					if(use.matches(password)){
						Messger[1] = use;
		System.out.println("正確,請輸入手機號碼:(只能是數字、長度在5到11之間)");
						break;
					}else{
			System.out.println("錯誤,請重新輸入,還有:"+i+" 次輸出機會");
					}
				}
			}
			
            if(Messger[1] == null){
            	System.out.println("錯誤,密碼信息未註冊完成");
			}else{
			//註冊手機號碼
			for(int i=5;i>0;i--){
				sc = new Scanner(System.in);
				String use = sc.nextLine();
				
				if(use.matches(number)){
					Messger[2] = use;
System.out.println("正確,請輸入QQ郵箱:(後綴必須是@qq.com,QQ號碼長度在5到11之間)");
					break;
				}else{
		System.out.println("錯誤,請重新輸入,還有:"+i+" 次輸出機會");
				    continue;
				}
			}
		}
         
           if(Messger[2] == null){
        	   System.out.println("錯誤,手機號碼信息未註冊完成");
			}else{
			//註冊QQ郵箱
			for(int i=5;i>0;i--){
				sc = new Scanner(System.in);
				String use = sc.nextLine();
				
				if(use.matches(email)){
					Messger[3] = use;
System.out.println("正確,請輸入地址:(不能包含空格、特殊標點符號,長度在2-24之間)");
					break;
				}else{
		System.out.println("錯誤,請重新輸入,還有:"+i+" 次輸出機會");
				    continue;
				}
			}
		}	
           
           if(Messger[3] == null){
        	   System.out.println("錯誤,QQ郵箱信息未註冊完成");
			}else{
			//註冊地址
			for(int i=5;i>0;i--){
				sc = new Scanner(System.in);
				String use = sc.nextLine();
				
				if(use.matches(address)){
					Messger[4] = use;
					System.out.println("註冊完成,請登錄。");
					break;
				}else{
			System.out.println("錯誤,請重新輸入,還有:"+i+" 次輸出機會");
				    continue;
				}
			}
		} //用戶名驗證結束
           
        if(Messger[4] == null){
        	   System.out.println("錯誤,地址信息未註冊完成");
	    }else{
			//註冊成功後登錄
			if(Messger.toString()==null){
				System.out.println("還未註冊,請先完成註冊。");
			}else{
				System.out.println("*****歡迎光臨登陸界面*****");
				System.out.println("請輸入用戶名:");
				//登錄用戶名驗證
				for(int i=5;i>0;i--){
					
					sc = new Scanner(System.in);
					String users = sc.nextLine();
					if(users.equals(Messger[0])){
						System.out.println("正確,請輸入密碼:");
						break;
					}else{
						System.out.println("用戶名輸入錯誤,還有:"+i+" 次輸出機會");
						continue;
					}
				}
			}	//用戶名驗證結束
				
			if(Messger[0] == null){
	        	   System.out.println("錯誤,請先輸入用戶名");
		    }else{	
				//登錄密碼驗證
                for(int i=5;i>0;i--){
					
					sc = new Scanner(System.in);
					String users = sc.nextLine();
					if(users.equals(Messger[1])){
						System.out.println("登錄成功,歡迎光臨。");
						break;
					}else{
						System.out.println("密碼輸入錯誤,還有:"+i+" 次輸出機會");
						continue;
					}
					
				}

			}//密碼驗證結束
	 }//用戶名,密碼驗證結束	
} //第一個if語句結束
		
		//成功登錄,選擇功能
		for(int i=10;i>0;i--){
			
System.out.println("輸入相應數字,選擇功能:1、查看用戶信息   2、刪除用戶信息   3、退出系統");
		sc = new Scanner(System.in);
		int cases = sc.nextInt();
		if(cases == 1){
			System.out.println("個人用戶信息如下:");
				for(String x:Messger){
						System.out.print(x+" ");
				}
			System.out.println(" ");
		}else if(cases == 2){
			System.out.println("用戶信息刪除成功");
			Messger = new String[5];
		}else if(cases == 3){
			System.out.println("系統退出成功。");
			break;
		}
		}//for結束
		
	}
}

代碼的執行結果:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章