All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.blade.kit.PatternKit Maven / Gradle / Ivy

package com.blade.kit;

import java.util.regex.Pattern;

/**
 * 正则工具类
 * 提供验证邮箱、手机号、电话号码、身份证号码、数字等方法
 *
 * @author biezhi
 * @since 1.0
 */
public final class PatternKit {

    private static final String EMAIL_REGEX  = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
    private static final String IP_REGEX     = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
    private static final String URL_REGEX    = "^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
    private static final String MOBILE_REGEX = "^(13[0-9]|14[57]|15[012356789]|17[0678]|18[0-9])[0-9]{8}$";

    /**
     * 验证Email
     *
     * @param email email地址,格式:[email protected][email protected],xxx代表邮件服务商
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean isEmail(String email) {
        return isMatch(EMAIL_REGEX, email);
    }

    /**
     * 验证身份证号码
     *
     * @param idCard 居民身份证号码18位,最后一位可能是数字或字母
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean isIdCard18(String idCard) {
        String regex = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
        return isMatch(regex, idCard);
    }

    /**
     * 验证身份证号码
     *
     * @param idCard 居民身份证号码15位,最后一位可能是数字或字母
     * @return 验证成功返回true,验证失败返回false
     */
    public static boolean isIdCard15(String idCard) {
        String regex = "^[1-9]\\\\d{7}((0\\\\d)|(1[0-2]))(([0|1|2]\\\\d)|3[0-1])\\\\d{3}$";
        return isMatch(regex, idCard);
    }

    public static boolean isImage(String suffix) {
        if (null != suffix && !"".equals(suffix) && suffix.contains(".")) {
            String regex = "(.*?)(?i)(jpg|jpeg|png|gif|bmp|webp)";
            return isMatch(regex, suffix);
        }
        return false;
    }

    /**
     * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))
     *
     * @param mobile 正则:手机号(精确)
     *               

移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188

*

联通:130、131、132、145、155、156、171、175、176、185、186

*

电信:133、153、173、177、180、181、189

*

全球星:1349

*

虚拟运营商:170

* @return 验证成功返回true,验证失败返回false */ public static boolean isMobile(String mobile) { return isMatch(MOBILE_REGEX, mobile); } /** * 验证固定电话号码 * * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447 *

国家(地区) 代码 :标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字, * 数字之后是空格分隔的国家(地区)代码。

*

区号(城市代码):这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号—— * 对不使用地区或城市代码的国家(地区),则省略该组件。

*

电话号码:这包含从 0 到 9 的一个或多个数字

* @return 验证成功返回true,验证失败返回false */ public static boolean isPhone(String phone) { return isMatch("(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$", phone); } /** * 验证整数(正整数和负整数) * * @param digit 一位或多位0-9之间的整数 * @return 验证成功返回true,验证失败返回false */ public static boolean isDigit(String digit) { String regex = "\\-?[1-9]\\d+"; return isMatch(regex, digit); } /** * 验证整数和浮点数(正负整数和正负浮点数) * * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30 * @return 验证成功返回true,验证失败返回false */ public static boolean isDecimals(String decimals) { String regex = "\\-?[1-9]\\d+(\\.\\d+)?"; return isMatch(regex, decimals); } /** * 验证空白字符 * * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B * @return 验证成功返回true,验证失败返回false */ public static boolean isBlankSpace(String blankSpace) { String regex = "\\s+"; return isMatch(regex, blankSpace); } /** * 验证中文 * * @param chinese 中文字符 * @return 验证成功返回true,验证失败返回false */ public static boolean isChinese(String chinese) { String regex = "^[\u4E00-\u9FA5]+$"; return isMatch(regex, chinese); } /** * 验证中文字母数字空格 * * @param chinese 中文字符 * @return 验证成功返回true,验证失败返回false */ public static boolean isRealName(String chinese) { String regex = "^[A-Za-z0-9\\s\u4E00-\u9FA5]+$"; return isMatch(regex, chinese); } /** * 检测是否是数字 */ public static boolean isNumber(String str) { String regex = "^[1-9]\\d*$"; return isMatch(regex, str); } /** * 验证日期(年月日) * * @param birthday 日期,格式:1992-09-03,或1992.09.03 * @return 验证成功返回true,验证失败返回false */ public static boolean isBirthday(String birthday) { String regex = "^(\\d{4})-(\\d{2})-(\\d{2})$"; return isMatch(regex, birthday); } /** * 验证URL地址 * * @param url 格式:http://biezhi.me:80 ftp://192.168.2.12 * @return 验证成功返回true,验证失败返回false */ public static boolean isURL(String url) { return isMatch(URL_REGEX, url); } /** * 匹配中国邮政编码 * * @param postcode 邮政编码 * @return 验证成功返回true,验证失败返回false */ public static boolean isPostcode(String postcode) { String regex = "[1-9]\\d{5}"; return isMatch(regex, postcode); } /** * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小) * * @param ipAddress IPv4标准地址 * @return 验证成功返回true,验证失败返回false */ public static boolean isIpAddress(String ipAddress) { return isMatch(IP_REGEX, ipAddress); } /** * 判断是否匹配正则 * * @param regex 正则表达式 * @param input 要匹配的字符串 * @return {@code true}: 匹配
{@code false}: 不匹配 */ public static boolean isMatch(final String regex, final CharSequence input) { return input != null && input.length() > 0 && Pattern.matches(regex, input); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy