How does Java use regular expressions to validate usernames and passwords
This article mainly introduces how Java uses regular expressions to verify user names and passwords, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Request:
User name:
Must be 6-10 letters, numbers, underscores (where letters, numbers, underscores refer to any combination, not all three must be included) cannot start with a number
Password:
Must be 6-20 digits of letters, numbers, underscores (here letters, numbers, underscores refer to any combination, there is no need to include all three categories)
Package cn.zyq.Aug05;import java.util.Scanner;public class Demo1 {public static void main (String [] args) {Scanner sc = new Scanner (System.in); System.out.println ("Please fill in the user name:"); String name = sc.next (); / * * verify the user name * while loop control until the input is legal * / while (! checkName (name)) {System.out.println ("invalid user name, please re-enter:") Name=sc.next ();} / * verify password * / System.out.println ("Please fill in password:"); String pwd=sc.next (); while (! checkPwd (pwd)) {System.out.println ("invalid password, please re-enter:"); pwd=sc.next ();} System.out.println ("registered successfully!") ;} / * * user name authentication * @ param name * @ return * / public static boolean checkName (String name) {String regExp = "^ [^ 0-9] [\\ w _] {5pm 9} $"; if (name.matches (regExp)) {return true;} else {return false }} / * password authentication * @ param pwd * @ return * / public static boolean checkPwd (String pwd) {String regExp = "^ [\\ w _] {6 return true; 20} $"; if (pwd.matches (regExp)) {return true;} return false }} Thank you for reading this article carefully. I hope the article "how to use regular expressions to verify user names and passwords with Java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!