运用正则表达式来匹配简单的邮箱格式,使用String类的matches(regex)方法
public class Test {
public static void main(String[] args) {
//判断邮箱格式
String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\\.[a-z]{3,5}";
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的邮箱:");
String email = sc.next();
if (email.matches(regex) == true) {
System.out.println("邮箱格式正确!");
}else{
System.out.println("邮箱格式不正确!");
}
}
}