一些我经过验证的-有效的-【正则表达式】-JAVA

在开发中,我们经常需要对参数做验证。需要用到一些正则表达式。这里我整理写我验证过的 正则表达式。

一:

 

 /**
     * <p>shang</p>
     * <p>判断email格式是否正确</p>
     */
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * <p>shang</p>
     * <p>判断手机格式是否正确</p>
     */
    public static boolean isPhone(String email) {
        String str = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * <p>shang</p>
     * <p>判断字符串是否是浮点类型</p>
     */
    public static boolean isPositiveDecimal(String email) {
        String str = "\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return !m.matches();
    }

 

二:

 

 /**
     * <p>用户昵称 判断</p>
     *
     * @return true 标示 包含的有非法字符
     * false 标示 没有包含非法字符
     */
    public static boolean checkStrContainIllegal(String str) {//& 这个符号为啥 在手机上 检测不到呢
        String regEx = "[^`~!@#$%^&*()+=|{}':;',\\[\\]<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,?¥&]*";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return !m.matches();
    }


    /**
     * <P> 只允许数字,英文,中文,下划线  </P>
     *
     * @return true 标示 包含的有非法字符
     * false 标示 没有包含非法字符
     */
    public static boolean checkWifiIllegal(String str) {
        String regEx = "^[\u4E00-\u9FA5a-zA-Z0-9_]*$";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return !m.matches();
    }

三 .下面是我从别人的Demo整理出来一部分

 

 

/**
     * 正则表达式:验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
    
     //正则:检测是否是Mac地址

     String patternMac="^([0-9a-fA-F]{2})(([/\\s:-][0-9a-fA-F]{2}){5})$";

    /**
     * 正则表达式:验证身份证
     */
    public static final String REGEX_ID_CARD = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X|x)$";

    /**
     * 正则表达式:验证港澳台证
     */
//    public static final String REGEX_ID_HKMT = "/(^[HMhm]{1}([0-9]{10}|[0-9]{8})$)|(^[0-9]{10}$)/";
    public static final String REGEX_ID_HKMT = "/([A-Z]{1,2}[0-9]{6}([0-9A]))|(^[1|5|7][0-9]{6}\\([0-9Aa]\\))|([A-Z][0-9]{9})/";
    /**
     * 正则表达式:验证护照
     */
//    public static final String REGEX_ID_PASSPORT = "/^[a-zA-Z0-9]{5,17}$/";
    public static final String REGEX_ID_PASSPORT = " /^1[45][0-9]{7}|([P|p|S|s]\\d{7})|([S|s|G|g]\\d{8})|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\\d{8})|([H|h|M|m]\\d{8,10})$/";
    /**
     * 正则表达式:验证军官证
     */
//    public static final String REGEX_ID_COO = " /^[a-zA-Z0-9]{7,21}$/";
    public static final String REGEX_ID_COO = " /[\\u4e00-\\u9fa5](字第){1}(\\d{4,8})(号?)$/";

    /**
     * 正则表达式:验证URL
     */
    public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

    /**
     * 正则表达式:验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";

    //************************************网上找的,一般难用到***********************************
    /**
     * 检查字符串中是否还有HTML标签
     */
    public static final String HTMLTAGHAS = "<(\\S*?)[^>]*>.*?</\\1>|<.*? />";
    /**
     * 检查IP是否合法
     */
    public static final String IPADRESS = "\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}";
    /**
     * 检查QQ号是否合法
     */
    public static final String QQCODE = "[1-9][0-9]{4,13}";
    /**
     * 检查邮编是否合法
     */
    public static final String POSTCODE = "[1-9]\\d{5}(?!\\d)";
    /**
     * 正整数
     */
    public static final String POSITIVE_INTEGER = "^[0-9]\\d*$";
    /**
     * 正浮点数
     */
    public static final String POSITIVE_FLOAT = "^[1-9]\\d*.\\d*|0.\\d*[0-9]\\d*$";
    /**
     * 整数或小数
     */
    public static final String POSITIVE_DOUBLE = "^[0-9]+(\\.[0-9]+)?$";
    /**
     * 年月日 2012-1-1,2012/1/1,2012.1.1
     */
    public static final String DATE_YMD = "^\\d{4}(\\-|\\/|.)\\d{1,2}\\1\\d{1,2}$";

以后开发中有新的 验证过-的正则式子也会及时更新到本章中。方便大家使用。
 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值