Java正则工具__手机号、身份证、车牌号……

你拥有青春的时候,你就要感受它。不要虚掷你的黄金时代,不要去倾听枯燥乏味的东西,不要设法挽留无望的失败,不要把你的生命献给无知、平庸和低俗。这些都是我们时代病态的目标、虚假的理想。活着,把你宝贵的内在生命活出来,什么都别错过。——王尔德

public class RegexUtil {

    private RegexUtil() {
    }

    /**
     * 手机号正则
     */
    private static final String PHONE = "^(13\\d|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18\\d|19[0-35-9])\\d{8}$";

    /**
     * 身份证号正则
     */
    private static final String IDENTITY = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";

    /**
     * 车牌号正则
     */
    private static final String VEHICLE = "^[京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云渝藏陕甘青宁新][A-Z][A-Z0-9]{5,6}$";

    /**
     * 汉字姓名(2-4个字)
     */
    private static final String CNAME = "^[\\u4e00-\\u9fa5]{2,4}$";


    /**
     * 是电话格式吗
     */
    public static void isPhoneFormat(String phone) {
        Assert.isTrue(!StringUtils.hasLength(phone), "电话号码 不能为空");
        Assert.isTrue(phone.matches(PHONE), "请输入正确的电话号码");
    }

    /**
     * 是身份证格式吗
     */
    public static void isIdentityFormat(String identity) {
        Assert.isTrue(!StringUtils.hasLength(identity), "身份证号码 不能为空");
        Assert.isTrue(identity.matches(IDENTITY), "请输入正确的身份证号码");
    }

    /**
     * 是车牌号格式吗
     */
    public static void isVehicleId(String vehicleId) {
        Assert.isTrue(!StringUtils.hasLength(vehicleId), "车牌号 不能为空");
        Assert.isTrue(vehicleId.matches(VEHICLE), "请输入正确的车牌号");
    }

    /**
     * 是中文姓名吗
     */
    public static void isChinesName(String chineseName) {
        Assert.isTrue(!StringUtils.hasLength(chineseName), "姓名 不能为空");
        Assert.isTrue(chineseName.matches(CNAME), "请输入2-4个字数的(中文)姓名");
    }

}

 

posted @ 2023-06-08 15:44  Ashe|||^_^  阅读(96)  评论(0编辑  收藏  举报