正则表达式(邮箱)

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的邮箱~");
        String str = scanner.next();
        isEmail(str);
    }
    public static void isEmail(String str) {
         String regex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";    
         Pattern m = Pattern.compile(regex);    
         Matcher matcher = m.matcher(str);    
         boolean flag = matcher.matches();    
        if(flag)
            System.out.println("邮箱格式正确!!");
        else {
            System.out.println("邮箱格式错误!!");
        }
    }

遇到正则表达式,我一般会选择百度~~~

posted @ 2019-11-21 20:45  James_Harden  阅读(235)  评论(0编辑  收藏  举报