public static void main(String[] args) {
// 定义号码规则
String regex = "[1][3458]\\d{4}(\\d)\\1{4}";
// 输入电话号码
Scanner numIn = new Scanner(System.in);
System.out.println("请输入电话号码:");
String str = numIn.next();
if (str.toCharArray().length == 11) {
if (str.matches(regex)) {
System.out.println("匹配成功!");
} else {
System.out.println("匹配错误!");
}
} else {
System.out.println("电话号码不正确!");
}
}
} |