Java常用正则表达式
生活随笔
收集整理的這篇文章主要介紹了
Java常用正则表达式
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.beike.springboot.other;import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** @ClassName: CommonRegex* @description: * @date: 2019/5/28 11:06*/
public class CommonRegex {/*** 驗(yàn)證Email** @param email email地址,格式:zhang@gmail.com,zhang@xxx.com.cn,xxx代表郵件服務(wù)商* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkEmail(String email) {String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";return Pattern.matches(regex, email);}/*** 驗(yàn)證身份證號(hào)碼** @param idCard 居民身份證號(hào)碼15位或18位,最后一位可能是數(shù)字或字母* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkIdCard(String idCard) {String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";return Pattern.matches(regex, idCard);}/*** 驗(yàn)證手機(jī)號(hào)碼(支持國(guó)際格式,+86135xxxx...(中國(guó)內(nèi)地),+00852137xxxx...(中國(guó)香港))** @param mobile 移動(dòng)、聯(lián)通、電信運(yùn)營(yíng)商的號(hào)碼段* 移動(dòng)的號(hào)段:134(0-8)、135、136、137、138、139、147(預(yù)計(jì)用于TD上網(wǎng)卡)* 、150、151、152、157(TD專用)、158、159、187(未啟用)、188(TD專用)* 聯(lián)通的號(hào)段:130、131、132、155、156(世界風(fēng)專用)、185(未啟用)、186(3g)* 電信的號(hào)段:133、153、180(未啟用)、189* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkMobile(String mobile) {String regex = "(\\+\\d+)?1[3458]\\d{9}$";return Pattern.matches(regex, mobile);}/*** 驗(yàn)證固定電話號(hào)碼** @param phone 電話號(hào)碼,格式:國(guó)家(地區(qū))電話代碼 + 區(qū)號(hào)(城市代碼) + 電話號(hào)碼,如:+8602085588447* 國(guó)家(地區(qū)) 代碼 :標(biāo)識(shí)電話號(hào)碼的國(guó)家(地區(qū))的標(biāo)準(zhǔn)國(guó)家(地區(qū))代碼。它包含從 0 到 9 的一位或多位數(shù)字,* 數(shù)字之后是空格分隔的國(guó)家(地區(qū))代碼。* 區(qū)號(hào)(城市代碼):這可能包含一個(gè)或多個(gè)從 0 到 9 的數(shù)字,地區(qū)或城市代碼放在圓括號(hào)——* 對(duì)不使用地區(qū)或城市代碼的國(guó)家(地區(qū)),則省略該組件。* 電話號(hào)碼:這包含從 0 到 9 的一個(gè)或多個(gè)數(shù)字* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkPhone(String phone) {String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";return Pattern.matches(regex, phone);}/*** 驗(yàn)證整數(shù)(正整數(shù)和負(fù)整數(shù))** @param digit 一位或多位0-9之間的整數(shù)* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkDigit(String digit) {String regex = "\\-?[1-9]\\d+";return Pattern.matches(regex, digit);}/*** 驗(yàn)證整數(shù)和浮點(diǎn)數(shù)(正負(fù)整數(shù)和正負(fù)浮點(diǎn)數(shù))** @param decimals 一位或多位0-9之間的浮點(diǎn)數(shù),如:1.23,233.30* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkDecimals(String decimals) {String regex = "\\-?[1-9]\\d+(\\.\\d+)?";return Pattern.matches(regex, decimals);}/*** 驗(yàn)證空白字符** @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkBlankSpace(String blankSpace) {String regex = "\\s+";return Pattern.matches(regex, blankSpace);}/*** 驗(yàn)證中文** @param chinese 中文字符* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkChinese(String chinese) {String regex = "^[\u4E00-\u9FA5]+$";return Pattern.matches(regex, chinese);}/*** 驗(yàn)證日期(年月日)** @param birthday 日期,格式:1992-09-03,或1992.09.03* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkBirthday(String birthday) {String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";return Pattern.matches(regex, birthday);}/*** 驗(yàn)證URL地址** @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkURL(String url) {String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";return Pattern.matches(regex, url);}/*** * 獲取網(wǎng)址 URL 的一級(jí)域名* http://detail.tmall.com/item.htm?spm=a230r.1.10.44.1xpDSH&id=15453106243&_u=f4ve1uq1092 ->> tmall.com** @param url* @return*/private static Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);public static String getDomain(String url) {// 獲取完整的域名// Pattern p=Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);Matcher matcher = p.matcher(url);matcher.find();return matcher.group();}/*** 匹配中國(guó)郵政編碼** @param postcode 郵政編碼* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkPostcode(String postcode) {String regex = "[1-9]\\d{5}";return Pattern.matches(regex, postcode);}/*** 匹配IP地址(簡(jiǎn)單匹配,格式,如:192.168.1.1,127.0.0.1,沒(méi)有匹配IP段的大小)** @param ipAddress IPv4標(biāo)準(zhǔn)地址* @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false*/public static boolean checkIpAddress(String ipAddress) {String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";return Pattern.matches(regex, ipAddress);}//是否包含 . 號(hào)public static boolean checkContainsDot(String username) {return username.contains(".");}//是否包含連詞符public static boolean checkContainsHyphen(String username) {return username.contains("-");}//密碼長(zhǎng)度 6-20public static boolean checkUserPasswordLength(String pwd) {return pwd.length() > 5 && pwd.length() < 21;}public static boolean isValidUserName(String un) {String regex = "([A-Z0-9a-z-]|[\\u4e00-\\u9fa5])+";return Pattern.matches(regex, un);}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/fanBlog/p/10935983.html
總結(jié)
以上是生活随笔為你收集整理的Java常用正则表达式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: RabbitMQ配置环境变量后启动不了的
- 下一篇: Java基础--多线程