邮箱格式正则校验

一、字符串是否为合法的邮箱格式

/**
     *利用正则表达式检查字符串是否为邮箱格式
     * @param email
     * @return
     */
    public static String isEmail(String email){
        String regex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        if(email.matches(regex)){
            return "邮箱格式正确";
        }else{
            return "邮箱格式错误";
        }
    }

二、字符串是否为合法的邮政编码格式

 /**
     * 利用正则表达式检查字符串是否为邮政编码格式
     * @param postCode
     * @return
     */
    public static String isPostCode(String postCode){
        String regex =  "[1-9]\\d{5}";
        if(postCode.matches(regex)){
            return "邮编格式正确";
        }else{
            return "邮编格式错误";
        }
    }

三、调用方法

public class EmailIDVerification {
    public static void main(String[] args) {
        //邮箱校验
        String email = "[email protected]";
        String result = EmailIDVerification.isEmail(email);
        System.out.println(email+result);

        //邮编校验
        String postcode = "100000";
        String result1 = EmailIDVerification.isPostCode(postcode);
        System.out.println(postcode+result1);
    }
}

你可能感兴趣的:(邮箱格式正则校验)