1 public class Demo02 { 2 public static void main(String[] args) { 3 Scanner sc = new Scanner(System.in); 4 String s = ""; 5 System.out.println("請輸入一個字符串"); 6 s = sc.nextLine(); 7 // System.out.println(checkQQ(s)); 8 System.out.println(checkEmail(s)); 9 } 10 11 // 利用正則表達式校驗QQ號 12 public static boolean checkQQ(String str) { 13 return str.matches("[1-9][0-9]{4,14}"); // matches()方法告知此字符串是否匹配給定的正則表達式。 14 } 15 16 // 校驗郵箱地址 17 public static boolean checkEmail(String email) { 18 return email.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"); 19 } 20 }