import java.util.Scanner; import java.util.Scanner; public class StringRegTest { public static void main(String[] args) { //1.定义描述规则的正则表达式字符串并使用变量记录 //String Reg = "[0-9]{6}"; //String Reg = "^[0-9]{6}$"; String Reg = "\\d{6}"; System.out.println("请输入您的银行卡密码!!!"); while (true){ Scanner sc =new Scanner(System.in); String str1= sc.next(); if(str1.matches(Reg)){ System.out.println("您输入的银行卡密码正确!!"); break; }else { System.out.println("您输入的银行卡格式不正确!!"); } } } }