Java使用正则表达式验证用户名和密码
要求:效果展示:源代码:要求:
用户名:
必须是6-10位字母、数字、下划线(这里字母、数字、下划线是指任意组合,没有必须三类均包含)不能以数字开头
密码:
必须是6-20位的字母、数字、下划线(这里字母、数字、下划线是指任意组合,没有必须三类均包含)
效果展示:
源代码:
package com.guo;import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请填写用户名:");String name = sc.next();/*** 验证用户名* while循环控制直到输入合法为止*/while(!checkName(name)) {System.out.println("用户名不合法,请重新输入:");name=sc.next();}/*** 验证密码*/System.out.println("请填写密码:");String pwd = sc.next();while(!checkPwd(pwd)) {System.out.println("密码不合法,请重新输入:");pwd=sc.next();}System.out.println("注册成功!");}/*** 用户名验证* @param name* @return*/public static boolean checkName(String name) {String regExp = "^[^0-9][\\w_]{5,9}$";if(name.matches(regExp)) {return true;}else {return false;}}/*** 密码验证* @param pwd* @return*/public static boolean checkPwd(String pwd) {String regExp = "^[\\w_]{6,20}$";if(pwd.matches(regExp)) {return true;}return false;}}