分享

多种写法表示-正则表达式之用户名、密码、手机号码、邮箱、地址的字符序列规则

 印度阿三17 2020-02-27

正则表达式之用户名的字符序列规则

用户名开头不能是数字,不能包含空格,特殊标点符号,长度在 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}";


正则表达式之密码的字符序列规则

密码开头第一位必须是大写字母,不能包含空格,特殊标点符号,汉字,长度在 2 到 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结束
		
	}
}

代码的执行结果:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多