邮箱格式正则校验

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

/**
     *利用正则表达式检查字符串是否为邮箱格式
     * @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 = "98565544@163.com";
        String result = EmailIDVerification.isEmail(email);
        System.out.println(email+result);

        //邮编校验
        String postcode = "100000";
        String result1 = EmailIDVerification.isPostCode(postcode);
        System.out.println(postcode+result1);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容