正則 只有英文或者數字 長度6位以上 數字或者英文全部一樣
阿新 • • 發佈:2020-12-07
要求
1 只有英文或者數字
2 長度大於6位
3不能全部是相同的數字或字母
解決:
自己寫了一個測試類
public class emailTest { public static void main(String[] args) { for(int i =0;i<10;i++){ Scanner scanner = new Scanner(System.in); //annotate 註解 System.out.println("請輸入oe"); String account = scanner.nextLine(); String regex1= "^[a-zA-Z0-9]{6,}$"; String regex2 = "^([0-9a-zA-Z])\\1+$"; Pattern pattern = Pattern.compile(regex1); Matcher matcher = pattern.matcher(account); Pattern pattern1 = Pattern.compile(regex2); Matcher matcher1 = pattern1.matcher(account); if (matcher1.matches()||!matcher.matches()) { System.out.println("oe碼不能為中文字元,長度大於等於6位,不能全部是相同的數字或字母"); } else { System.out.println("正確"); } } } }
輸出: