一些我經過驗證的-有效的-【正則表示式】-JAVA
阿新 • • 發佈:2018-12-11
在開發中,我們經常需要對引數做驗證。需要用到一些正則表示式。這裡我整理寫我驗證過的 正則表示式。
一:
/** * <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(); }
二:
三 .下面是我從別人的Demo整理出來一部分/** * <p>使用者暱稱 判斷</p> * * @return true 標示 包含的有非法字元 * false 標示 沒有包含非法字元 */ public static boolean checkStrContainIllegal(String str) {//& 這個符號為啥 在手機上 檢測不到呢 String regEx = "[^`[email protected]#$%^&*()+=|{}':;',\\[\\]<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,?¥&]*"; 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(); }
以後開發中有新的 驗證過-的正則式子也會及時更新到本章中。方便大家使用。/** * 正則表示式:驗證漢字 */ public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$"; /** * 正則表示式:驗證身份證 */ 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}$";