import java.util.Scanner; public class Test { public static void main(String[] args) { //用正则验证 用户输入的手机号和QQ邮箱 checkPhone(); checkEmail(); } public static void checkEmail(){ Scanner sc = new Scanner(System.in); while (true) { System.out.println("请您输入您的注册邮箱:"); String email = sc.next(); if(email.matches("\\w{1,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]{2,20}){1,2}")){ System.out.println("邮箱格式正确,注册完成!"); break; }else { System.out.println("格式有误!"); } } } public static void checkPhone(){ Scanner s=new Scanner(System.in); System.out.println("请输入11为手机号码:"); String res= s.next(); //判断设手机号码格式是否正确 if (res.matches("1[3-9]\\d{9}")){ System.out.println("你的手机号码为:"+res); }else{ System.out.println("你输入的手机号码不合法!!!"); } } }
Java用正则表达式验证用户输入的手机号和QQ邮箱
最新推荐文章于 2024-04-28 10:35:01 发布