1. 程式人生 > >Java 正則校驗(手機,郵箱,固話........)

Java 正則校驗(手機,郵箱,固話........)

public class RegexUtils {
    public static final String Email = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+([a-zA-Z]{2,6}|中國|公司|網路)$";
    public static final String Phone = "^1[34578]\\d{9}$";
    public static final String Tel = "^0[1-9](\\d{1,2}\\-?)\\d{7,8}"
; public static final String Url = "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^]\\s`!()\\[{};:'\".,<>?«»“”‘’]))"; public
static final String Domain = "^http://(?!\\-)(?:[a-zA-Z\\d\\-]{0,62}[a-zA-Z\\d]\\.){1,126}(?!\\d+)[a-zA-Z\\d]{1,63}$"; public static final String ImgUrl = "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^]\\s`!()\\[{};:'\".,<>?«»“”‘’]))[.](jpg|jpeg|png|gif|bmp)"
; public static final String Number = "^-?(?:\\d+|\\d{1,3}(?:,\\d{3})+)?(?:\\.\\d+)?$"; public static final String Chinese = "[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+"; public static final String PostCode = "^(\\d|\\d{2})\\d4$"; public static final String IdNo = "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})(([1|2][0-9]{3}[0|1][0-9][0-3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|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$"; public static final String ShortDate = "^\\d{4}[\\-](0?[1-9]|1[012])[\\-](0?[1-9]|[12][0-9]|3[01])$"; public static final String LongDate = "^\\d{4}[\\-](0?[1-9]|1[012])[\\-](0?[1-9]|[12][0-9]|3[01]) (?:2[0-3]|[01]?[0-9]):[0-5][0-9]:[0-5][0-9]$"; public static final String QQ = "^[1-9][0-9]{4,9}$"; public static final String Letter = "^[A-Za-z]+$"; public static final String CapitalLetter = "^[A-Z]+$"; public static final String LowerCaseLetter = "^[a-z]+$"; public static final String LetterDigit = "^[0-9A-Za-z]+$"; public static final String Color = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"; public static final String URL_IMAGE = "^(http://)?([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$"; public static boolean matchEmail(String value) { return match(RegexUtils.Email, value); } /** * 1 移動號段:135、136、137、138、139、147、150、151、152、157、158、159、182、183、184、187、188。 * 2 聯通號段:130、131、132、145、155、156、185、186、170 * 3 電訊號段:133 153 180 181 189 177 */ public static boolean matchPhone(String value) { return match(RegexUtils.Phone, value); } public static boolean matchTel(String value) { return match(RegexUtils.Tel, value); } public static boolean matchUrl(String value) { return match(RegexUtils.Url, value); } public static boolean matchDomain(String value) { return match(RegexUtils.Domain, value); } public static boolean matchImageUrl(String value) { return match(RegexUtils.ImgUrl, value); } public static boolean matchNumber(String value) { return match(RegexUtils.Number, value); } public static boolean matchChinese(String value) { return match(RegexUtils.Chinese, value); } // 第一位數字索引 // 包含地區名稱和郵編,按開頭的第一位數字分: // 0、1、2、3、4、5、6、7、8、9。 // // // 前兩位數字索引 // 按開頭的前兩位數字分: // 00、01、02、03、04、05、06、07、08、09 // 10、11、12、13、14、15、16、17、18、19 // 20、21、22、23、24、25、26、27、28、29 // 30、31、32、33、34、35、36、37、38、39 // 40、41、42、43、44、45、46、47、48、49 // 50、51、52、53、54、55、56、57、58、59 // 60、61、62、63、64、65、66、67、68、69 // 70、71、72、73、74、75、76、77、78、79 // 80、81、82、83、84、85、86、87、88、89 // 90、91、92、93、94、95、96、97、98、99 public static boolean matchPostCode(String value) { return match(RegexUtils.PostCode, value); } public static boolean matchIdNo(String value) { return match(RegexUtils.IdNo, value); } // 類別 1st八位位元組十進位制範圍 1st 八位位元組高階位 網路/主機ID (N=網路, H=主機) 預設子網掩碼 網路數量 每個網路的主機(可用地址) // A 1 – 126* 0 N.H.H.H 255.0.0.0 126 (27 – 2) 16,777,214 (224 – 2) // B 128 – 191 10 N.N.H.H 255.255.0.0 16,382 (214 – 2) 65,534 (216 – 2) // C 192 – 223 110 N.N.N.H 255.255.255.0 2,097,150 (221 – 2) 254 (28 – 2) // D 224 – 239 1110 為多播預留 // E 240 – 254 1111 實驗性的,用於研究 // 注意:A類IP地址中127.0.0.0 至 127.255.255.255 不能被使用,它們預留用於環回地址和診斷功能。 // // 私有網路地址 // // 類別 私有網路 子網掩碼 地址範圍 // A 10.0.0.0 255.0.0.0 10.0.0.0 - 10.255.255.255 // B 172.16.0.0 - 172.31.0.0 255.240.0.0 172.16.0.0 - 172.31.255.255 // C 192.168.0.0 255.255.0.0 192.168.0.0 - 192.168.255.255 public static boolean matchIp(String value) { return match(RegexUtils.Ip, value); } public static boolean matchShortDate(String value) { return match(RegexUtils.ShortDate, value); } public static boolean matchLongDate(String value) { return match(RegexUtils.LongDate, value); } public static boolean matchQQ(String value) { return match(RegexUtils.QQ, value); } public static boolean matchLetter(String value) { return match(RegexUtils.Letter, value); } public static boolean matchCapitalLetter(String value) { return match(RegexUtils.CapitalLetter, value); } public static boolean matchLowerCaseLetter(String value) { return match(RegexUtils.LowerCaseLetter, value); } public static boolean matchLetterDigit(String value) { return match(RegexUtils.LetterDigit, value); } public static boolean matchColor(String value) { return match(RegexUtils.Color, value); } public static boolean match(String regex, String value) { if (StringUtils.isEmpty(value)) { return false; } Pattern pattern = Pattern.compile(regex); return pattern.matcher(value).matches(); } public static boolean find(String regex, String value) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(value); return matcher.find(); } }