RegularUtils.java 3.5 KB
package com.sunvote.util;

import java.util.regex.Pattern;
/**
 * Created by Elvis on 2017/8/15 15:03
 * Email:Eluis@psunsky.com
 * 版权所有:长沙中天电子设计开发有限公司
 * Description: 工具包
 */
public class RegularUtils {

    private RegularUtils() {
        throw  new UnsupportedOperationException("No Impl");
    }

    /**
     * 验证手机号(简单)
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isMobileSimple(String string) {
        return isMatch(ConstUtils.REGEX_MOBILE_SIMPLE, string);
    }

    /**
     * 验证手机号(精确)
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isMobileExact(String string) {
        return isMatch(ConstUtils.REGEX_MOBILE_EXACT, string);
    }

    /**
     * 验证电话号码
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isTel(String string) {
        return isMatch(ConstUtils.REGEX_TEL, string);
    }

    /**
     * 验证身份证号码15位
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isIDCard15(String string) {
        return isMatch(ConstUtils.REGEX_IDCARD15, string);
    }

    /**
     * 验证身份证号码18位
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isIDCard18(String string) {
        return isMatch(ConstUtils.REGEX_IDCARD18, string);
    }

    /**
     * 验证邮箱
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isEmail(String string) {
        return isMatch(ConstUtils.REGEX_EMAIL, string);
    }

    /**
     * 验证URL
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isURL(String string) {
        return isMatch(ConstUtils.REGEX_URL, string);
    }

    /**
     * 验证汉字
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isChz(String string) {
        return isMatch(ConstUtils.REGEX_CHZ, string);
    }

    /**
     * 验证用户名
     * <p>取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾,用户名必须是6-20位</p>
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isUsername(String string) {
        return isMatch(ConstUtils.REGEX_USERNAME, string);
    }

    /**
     * 验证yyyy-MM-dd格式的日期校验,已考虑平闰年
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isDate(String string) {
        return isMatch(ConstUtils.REGEX_DATE, string);
    }

    /**
     * 验证IP地址
     *
     * @param string 待验证文本
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isIP(String string) {
        return isMatch(ConstUtils.REGEX_IP, string);
    }

    /**
     * string是否匹配regex
     *
     * @param regex  正则表达式字符串
     * @param string 要匹配的字符串
     * @return true: 匹配<br>false: 不匹配
     */
    public static boolean isMatch(String regex, String string) {
        return !StringUtils.isEmpty(string) && Pattern.matches(regex, string);
    }
}