1. 程式人生 > >正則工具類 -- RegexUtils

正則工具類 -- RegexUtils

-- ram 正則匹配 浮點數 simple div 電信 工具類 運營商

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {

    /**
     * 正則:手機號(簡單)
     */
    public static final String REGEX_MOBILE_SIMPLE = "^[1]\\d{10}$";
    /**
     * 正則:手機號(精確)
     * <p>移動:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188</p>
     * <p>聯通:130、131、132、145、155、156、175、176、185、186</p>
     * <p>電信:133、153、173、177、180、181、189</p>
     * <p>全球星:1349</p>
     * <p>虛擬運營商:170</p>
     
*/ public static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$"; /** * 正則:電話號碼 */ public static final String REGEX_TEL = "^0\\d{2,3}[- ]?\\d{7,8}"; /** * 正則:身份證號碼15位 */ public static final String REGEX_ID_CARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
/** * 正則:身份證號碼18位 */ public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$"; /** * 正則:郵箱 */ public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; /** * 正則:URL
*/ public static final String REGEX_URL = "[a-zA-z]+://[^\\s]*"; /** * 正則:漢字 */ public static final String REGEX_ZH = "^[\\u4e00-\\u9fa5]+$"; /** * 正則:用戶名,取值範圍為a-z,A-Z,0-9,"_",漢字,不能以"_"結尾,用戶名必須是6-20位 */ public static final String REGEX_USERNAME = "^[\\w\\u4e00-\\u9fa5]{6,20}(?<!_)$"; /** * 正則:yyyy-MM-dd格式的日期校驗,已考慮平閏年 */ public static final String REGEX_DATE = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$"; /** * 正則:IP地址 */ public static final String REGEX_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; /** * 正則:雙字節字符(包括漢字在內) */ public static final String REGEX_DOUBLE_BYTE_CHAR = "[^\\x00-\\xff]"; /** * 正則:空白行 */ public static final String REGEX_BLANK_LINE = "\\n\\s*\\r"; /** * 正則:QQ號 */ public static final String REGEX_TENCENT_NUM = "[1-9][0-9]{4,}"; /** * 正則:中國郵政編碼 */ public static final String REGEX_ZIP_CODE = "[1-9]\\d{5}(?!\\d)"; /** * 正則:正整數 */ public static final String REGEX_POSITIVE_INTEGER = "^[1-9]\\d*$"; /** * 正則:負整數 */ public static final String REGEX_NEGATIVE_INTEGER = "^-[1-9]\\d*$"; /** * 正則:整數 */ public static final String REGEX_INTEGER = "^-?[1-9]\\d*$"; /** * 正則:非負整數(正整數 + 0) */ public static final String REGEX_NOT_NEGATIVE_INTEGER = "^[1-9]\\d*|0$"; /** * 正則:非正整數(負整數 + 0) */ public static final String REGEX_NOT_POSITIVE_INTEGER = "^-[1-9]\\d*|0$"; /** * 正則:正浮點數 */ public static final String REGEX_POSITIVE_FLOAT = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$"; /** * 正則:負浮點數 */ public static final String REGEX_NEGATIVE_FLOAT = "^-[1-9]\\d*\\.\\d*|-0\\.\\d*[1-9]\\d*$"; private RegexUtils() { throw new UnsupportedOperationException("u can‘t instantiate me..."); } /** * 驗證手機號(簡單) * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isMobileSimple(CharSequence input) { return isMatch(REGEX_MOBILE_SIMPLE, input); } /** * 驗證手機號(精確) * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isMobileExact(CharSequence input) { return isMatch(REGEX_MOBILE_EXACT, input); } /** * 驗證電話號碼 * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isTel(CharSequence input) { return isMatch(REGEX_TEL, input); } /** * 驗證身份證號碼15位 * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isIDCard15(CharSequence input) { return isMatch(REGEX_ID_CARD15, input); } /** * 驗證身份證號碼18位 * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isIDCard18(CharSequence input) { return isMatch(REGEX_ID_CARD18, input); } /** * 驗證郵箱 * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isEmail(CharSequence input) { return isMatch(REGEX_EMAIL, input); } /** * 驗證URL * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isURL(CharSequence input) { return isMatch(REGEX_URL, input); } /** * 驗證漢字 * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isZh(CharSequence input) { return isMatch(REGEX_ZH, input); } /** * 驗證用戶名 * <p>取值範圍為a-z,A-Z,0-9,"_",漢字,不能以"_"結尾,用戶名必須是6-20位</p> * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isUsername(CharSequence input) { return isMatch(REGEX_USERNAME, input); } /** * 驗證yyyy-MM-dd格式的日期校驗,已考慮平閏年 * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isDate(CharSequence input) { return isMatch(REGEX_DATE, input); } /** * 驗證IP地址 * * @param input 待驗證文本 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isIP(CharSequence input) { return isMatch(REGEX_IP, input); } /** * 判斷是否匹配正則 * * @param regex 正則表達式 * @param input 要匹配的字符串 * @return {@code true}: 匹配<br>{@code false}: 不匹配 */ public static boolean isMatch(String regex, CharSequence input) { return input != null && input.length() > 0 && Pattern.matches(regex, input); } /** * 獲取正則匹配的部分 * * @param regex 正則表達式 * @param input 要匹配的字符串 * @return 正則匹配的部分 */ public static List<String> getMatches(String regex, CharSequence input) { if (input == null) return null; List<String> matches = new ArrayList<>(); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); while (matcher.find()) { matches.add(matcher.group()); } return matches; } /** * 獲取正則匹配分組 * * @param input 要分組的字符串 * @param regex 正則表達式 * @return 正則匹配分組 */ public static String[] getSplits(String input, String regex) { if (input == null) return null; return input.split(regex); } /** * 替換正則匹配的第一部分 * * @param input 要替換的字符串 * @param regex 正則表達式 * @param replacement 代替者 * @return 替換正則匹配的第一部分 */ public static String getReplaceFirst(String input, String regex, String replacement) { if (input == null) return null; return Pattern.compile(regex).matcher(input).replaceFirst(replacement); } /** * 替換所有正則匹配的部分 * * @param input 要替換的字符串 * @param regex 正則表達式 * @param replacement 代替者 * @return 替換所有正則匹配的部分 */ public static String getReplaceAll(String input, String regex, String replacement) { if (input == null) return null; return Pattern.compile(regex).matcher(input).replaceAll(replacement); } }

原文:http://www.jianshu.com/p/583998f435d0

正則工具類 -- RegexUtils