001/** 002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.jboot.web.validate; 017 018/** 019 * 正则表达式大全 020 * @author michael yang (fuhai999@gmail.com) 021 */ 022public class Regex { 023 024 /** 025 * 汉字 026 */ 027 public static final String CHINESE ="^[\\u4e00-\\u9fa5]+$"; 028 029 /** 030 * 全部是英文 031 */ 032 public static final String ENGLISH ="^[A-Za-z]+$"; 033 034 035 /** 036 * 英文或者数字 037 */ 038 public static final String ENGLISH_NUMBERS ="^[A-Za-z0-9]+$"; 039 040 041 /** 042 * 英文、数字 或下划线 043 */ 044 public static final String ENGLISH_NUMBERS_UNDERLINE ="^[A-Za-z0-9_]+$"; 045 046 047 048 /** 049 * 中文、英文、数字、或下划线 050 */ 051 public static final String CHINESE_ENGLISH_NUMBERS_UNDERLINE ="^[\\u4E00-\\u9FA5A-Za-z0-9_]+$"; 052 053 /** 054 * 正数、负数 或 小数 055 */ 056 public static final String DECIMAL ="^(\\-|\\+)?\\d+(\\.\\d+)?$"; 057 058 059 /** 060 * 密码长度 6~20 位数,字母、数字和下划线 061 */ 062 public static final String CIPHER ="^[a-zA-Z0-9_]\\w{5,19}$"; 063 064 065 /** 066 * 邮件地址 067 */ 068 public static final String EMAIL ="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"; 069 070 071 /** 072 * 域名 073 */ 074 public static final String DOMAIN ="^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}"; 075 076 /** 077 * url 地址 078 */ 079 public static final String URL ="[a-zA-z]+://[^\\s]*"; 080 081 /** 082 * 手机号码 083 */ 084 public static final String MOBILE ="^(1[3,4,5,6,7,8,9])\\d{9}$"; 085 086 /** 087 * 电话号码 088 */ 089 public static final String TELEPHONE ="^(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{7,14}$"; 090 091 /** 092 * 身份证号码 093 */ 094 public static final String ID_CARD ="^[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$"; 095 096 097 /** 098 * 日期格式 2020-02-0 099 */ 100 public static final String DATE ="^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$"; 101 102 103 /** 104 * 日期格式 2020-02-02 23:12:23 或者 2020-02-02 23:12 105 */ 106 public static final String DATE_TIME ="^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d(:[0-5]\\d)?$"; 107 108 109 /** 110 * 时间格式(没有秒) 2020-02-02 23:12 111 */ 112 public static final String DATE_TIME_HM ="^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d$"; 113 114 115 116 /** 117 * 时间格式 2020-02-02 23:12:23 118 */ 119 public static final String DATE_TIME_HMS ="^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$"; 120 121 122 123 /** 124 * ip地址 125 */ 126 public static final String IP ="^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"; 127 128 129 130 public static void main(String[] args){ 131 132 System.out.println("\"汉字\".matches(CHINESE) ---> " + ("汉字".matches(CHINESE))); 133 System.out.println("\"汉字123\".matches(CHINESE) ---> " + ("汉字123".matches(CHINESE))); 134 System.out.println(); 135 136 137 138 System.out.println("\"abc\".matches(ENGLISH) ---> " + ("abc".matches(ENGLISH))); 139 System.out.println("\"abc123\".matches(ENGLISH) ---> " + ("abc123".matches(ENGLISH))); 140 System.out.println(); 141 142 143 144 System.out.println("\"abc123\".matches(ENGLISH_NUMBERS) ---> " + ("abc123".matches(ENGLISH_NUMBERS))); 145 System.out.println("\"abc_123\".matches(ENGLISH_NUMBERS) ---> " + ("abc_123".matches(ENGLISH_NUMBERS))); 146 System.out.println("\"汉字abc123\".matches(ENGLISH_NUMBERS) ---> " + ("汉字汉字abc123".matches(ENGLISH_NUMBERS))); 147 System.out.println(); 148 149 150 151 System.out.println("\"abc123\".matches(ENGLISH_NUMBERS_UNDERLINE) ---> " + ("abc123".matches(ENGLISH_NUMBERS_UNDERLINE))); 152 System.out.println("\"abc_123\".matches(ENGLISH_NUMBERS_UNDERLINE) ---> " + ("abc_123".matches(ENGLISH_NUMBERS_UNDERLINE))); 153 System.out.println("\"汉字abc123\".matches(ENGLISH_NUMBERS_UNDERLINE) ---> " + ("汉字汉字abc123".matches(ENGLISH_NUMBERS_UNDERLINE))); 154 System.out.println(); 155 156 157 158 System.out.println("\"abc123\".matches(CHINESE_ENGLISH_NUMBERS_UNDERLINE) ---> " + ("abc123".matches(CHINESE_ENGLISH_NUMBERS_UNDERLINE))); 159 System.out.println("\"abc_123\".matches(CHINESE_ENGLISH_NUMBERS_UNDERLINE) ---> " + ("abc_123".matches(CHINESE_ENGLISH_NUMBERS_UNDERLINE))); 160 System.out.println("\"汉字abc123\".matches(CHINESE_ENGLISH_NUMBERS_UNDERLINE) ---> " + ("汉字汉字abc123".matches(CHINESE_ENGLISH_NUMBERS_UNDERLINE))); 161 System.out.println("\"汉字abc123#\".matches(CHINESE_ENGLISH_NUMBERS_UNDERLINE) ---> " + ("汉字abc123#".matches(CHINESE_ENGLISH_NUMBERS_UNDERLINE))); 162 System.out.println(); 163 164 165 166 System.out.println("\"汉字\".matches(DECIMAL) ---> " + ("汉字".matches(DECIMAL))); 167 System.out.println("\"123\".matches(DECIMAL) ---> " + ("123".matches(DECIMAL))); 168 System.out.println("\"123.12\".matches(DECIMAL) ---> " + ("123.12".matches(DECIMAL))); 169 System.out.println("\"-12\".matches(DECIMAL) ---> " + ("-12".matches(DECIMAL))); 170 System.out.println("\"-12.12\".matches(DECIMAL) ---> " + ("-12.12".matches(DECIMAL))); 171 System.out.println(); 172 173 174 175 System.out.println("\"123\".matches(PASSWORD) ---> " + ("123".matches(CIPHER))); 176 System.out.println("\"123456\".matches(PASSWORD) ---> " + ("123456".matches(CIPHER))); 177 System.out.println(); 178 179 180 181 System.out.println("\"汉字\".matches(EMAIL) ---> " + ("汉字".matches(EMAIL))); 182 System.out.println("\"abc\".matches(EMAIL) ---> " + ("abc".matches(EMAIL))); 183 System.out.println("\"abc@gmail\".matches(EMAIL) ---> " + ("bc.abc@gmail".matches(EMAIL))); 184 System.out.println("\"abc.abc@gmail\".matches(EMAIL) ---> " + ("bc.abc@gmail".matches(EMAIL))); 185 System.out.println("\"abc@gmail.com\".matches(EMAIL) ---> " + ("bc.abc@gmail.com".matches(EMAIL))); 186 System.out.println("\"abc.abc@gmail.com\".matches(EMAIL) ---> " + ("bc.abc@gmail.com".matches(EMAIL))); 187 System.out.println(); 188 189 190 191 System.out.println("\"yangfuhai\".matches(DOMAIN) ---> " + ("yangfuhai".matches(DOMAIN))); 192 System.out.println("\"yangfuhai.com\".matches(DOMAIN) ---> " + ("yangfuhai.com".matches(DOMAIN))); 193 System.out.println("\"www.yangfuhai.com\".matches(DOMAIN) ---> " + ("www.yangfuhai.com".matches(DOMAIN))); 194 System.out.println("\"http://www.yangfuhai.com\".matches(DOMAIN) ---> " + ("http://www.yangfuhai.com".matches(DOMAIN))); 195 System.out.println("\"http://www.yangfuhai.com/\".matches(DOMAIN) ---> " + ("http://www.yangfuhai.com/".matches(DOMAIN))); 196 System.out.println(); 197 198 199 200 System.out.println("\"汉字\".matches(URL) ---> " + ("汉字".matches(URL))); 201 System.out.println("\"abc\".matches(URL) ---> " + ("abc".matches(URL))); 202 System.out.println("\"http://www\".matches(URL) ---> " + ("http://www".matches(URL))); 203 System.out.println("\"http://www.yangfuhai.com\".matches(URL) ---> " + ("http://www.yangfuhai.com".matches(URL))); 204 System.out.println(); 205 206 207 208 System.out.println("\"汉字\".matches(MOBILE) ---> " + ("汉字".matches(MOBILE))); 209 System.out.println("\"12345\".matches(MOBILE) ---> " + ("12345".matches(MOBILE))); 210 System.out.println("\"11611223344\".matches(MOBILE) ---> " + ("11611223344".matches(MOBILE))); 211 System.out.println("\"12611223344\".matches(MOBILE) ---> " + ("12611223344".matches(MOBILE))); 212 System.out.println("\"13611223344\".matches(MOBILE) ---> " + ("13611223344".matches(MOBILE))); 213 System.out.println("\"14611223344\".matches(MOBILE) ---> " + ("14611223344".matches(MOBILE))); 214 System.out.println("\"15611223344\".matches(MOBILE) ---> " + ("15611223344".matches(MOBILE))); 215 System.out.println("\"16611223344\".matches(MOBILE) ---> " + ("16611223344".matches(MOBILE))); 216 System.out.println("\"17611223344\".matches(MOBILE) ---> " + ("17611223344".matches(MOBILE))); 217 System.out.println("\"18611223344\".matches(MOBILE) ---> " + ("18611223344".matches(MOBILE))); 218 System.out.println("\"19611223344\".matches(MOBILE) ---> " + ("19611223344".matches(MOBILE))); 219 System.out.println("\"196112233441\".matches(MOBILE) ---> " + ("196112233441".matches(MOBILE))); 220 System.out.println(); 221 222 223 224 System.out.println("\"汉字\".matches(TELEPHONE) ---> " + ("汉字".matches(TELEPHONE))); 225 System.out.println("\"021-1234567\".matches(TELEPHONE) ---> " + ("021-1234567".matches(TELEPHONE))); 226 System.out.println("\"0855-1234567\".matches(TELEPHONE) ---> " + ("0855-1234567".matches(TELEPHONE))); 227 System.out.println("\"1234567\".matches(TELEPHONE) ---> " + ("1234567".matches(TELEPHONE))); 228 System.out.println(); 229 230 231 232 System.out.println("\"汉字\".matches(ID_CARD) ---> " + ("汉字".matches(ID_CARD))); 233 System.out.println("\"522\".matches(ID_CARD) ---> " + ("522".matches(ID_CARD))); 234 System.out.println("\"522000000000000000\".matches(ID_CARD) ---> " + ("522000000000000000".matches(ID_CARD))); 235 System.out.println("\"52260119000125205x\".matches(ID_CARD) ---> " + ("52260119000125205x".matches(ID_CARD))); 236 System.out.println(); 237 238 239 240 System.out.println("\"汉字\".matches(DATE) ---> " + ("汉字".matches(DATE))); 241 System.out.println("\"abc\".matches(DATE) ---> " + ("abc".matches(DATE))); 242 System.out.println("\"123\".matches(DATE) ---> " + ("123".matches(DATE))); 243 System.out.println("\"2020-02-02\".matches(DATE) ---> " + ("2020-02-02".matches(DATE))); 244 System.out.println("\"2020-02-02 \".matches(DATE) ---> " + ("2020-02-02 ".matches(DATE))); 245 System.out.println("\"2020-02-02 23:32\".matches(DATE) ---> " + ("2020-02-02 23:32".matches(DATE))); 246 System.out.println("\"2020-02-02 23:32:21\".matches(DATE) ---> " + ("2020-02-02 23:32:21".matches(DATE))); 247 System.out.println(); 248 249 250 251 System.out.println("\"汉字\".matches(DATE_TIME) ---> " + ("汉字".matches(DATE_TIME))); 252 System.out.println("\"abc\".matches(DATE_TIME) ---> " + ("abc".matches(DATE_TIME))); 253 System.out.println("\"123\".matches(DATE_TIME) ---> " + ("123".matches(DATE_TIME))); 254 System.out.println("\"2020-02-02\".matches(DATE_TIME) ---> " + ("2020-02-02".matches(DATE_TIME))); 255 System.out.println("\"2020-02-02 \".matches(DATE_TIME) ---> " + ("2020-02-02 ".matches(DATE_TIME))); 256 System.out.println("\"2020-02-02 23:32\".matches(DATE_TIME) ---> " + ("2020-02-02 23:32".matches(DATE_TIME))); 257 System.out.println("\"2020-02-02 23:32:21\".matches(DATE_TIME) ---> " + ("2020-02-02 23:32:21".matches(DATE_TIME))); 258 System.out.println(); 259 260 261 262 System.out.println("\"汉字\".matches(DATE_TIME_HM) ---> " + ("汉字".matches(DATE_TIME_HM))); 263 System.out.println("\"abc\".matches(DATE_TIME_HM) ---> " + ("abc".matches(DATE_TIME_HM))); 264 System.out.println("\"123\".matches(DATE_TIME_HM) ---> " + ("123".matches(DATE_TIME_HM))); 265 System.out.println("\"2020-02-02\".matches(DATE_TIME_HM) ---> " + ("2020-02-02".matches(DATE_TIME_HM))); 266 System.out.println("\"2020-02-02 \".matches(DATE_TIME_HM) ---> " + ("2020-02-02 ".matches(DATE_TIME_HM))); 267 System.out.println("\"2020-02-02 23:32\".matches(DATE_TIME_HM) ---> " + ("2020-02-02 23:32".matches(DATE_TIME_HM))); 268 System.out.println("\"2020-02-02 23:32:21\".matches(DATE_TIME_HM) ---> " + ("2020-02-02 23:32:21".matches(DATE_TIME_HM))); 269 System.out.println(); 270 271 272 273 System.out.println("\"汉字\".matches(DATE_TIME_HMS) ---> " + ("汉字".matches(DATE_TIME_HMS))); 274 System.out.println("\"abc\".matches(DATE_TIME_HMS) ---> " + ("abc".matches(DATE_TIME_HMS))); 275 System.out.println("\"123\".matches(DATE_TIME_HMS) ---> " + ("123".matches(DATE_TIME_HMS))); 276 System.out.println("\"2020-02-02\".matches(DATE_TIME_HMS) ---> " + ("2020-02-02".matches(DATE_TIME_HMS))); 277 System.out.println("\"2020-02-02 \".matches(DATE_TIME_HMS) ---> " + ("2020-02-02 ".matches(DATE_TIME_HMS))); 278 System.out.println("\"2020-02-02 23:32\".matches(DATE_TIME_HMS) ---> " + ("2020-02-02 23:32".matches(DATE_TIME_HMS))); 279 System.out.println("\"2020-02-02 23:32:21\".matches(DATE_TIME_HMS) ---> " + ("2020-02-02 23:32:21".matches(DATE_TIME_HMS))); 280 System.out.println(); 281 282 283 284 System.out.println("\"汉字\".matches(IP) ---> " + ("汉字".matches(IP))); 285 System.out.println("\"abc\".matches(IP) ---> " + ("abc".matches(IP))); 286 System.out.println("\"123\".matches(IP) ---> " + ("123".matches(IP))); 287 System.out.println("\"123.123.123\".matches(IP) ---> " + ("123.123.123".matches(IP))); 288 System.out.println("\"123.123.123.123\".matches(IP) ---> " + ("123.123.123.123".matches(IP))); 289 System.out.println("\"255.255.255.255\".matches(IP) ---> " + ("255.255.255.255".matches(IP))); 290 System.out.println("\"255.255.256.255\".matches(IP) ---> " + ("255.255.256.255".matches(IP))); 291 System.out.println("\"0.0.0.0\".matches(IP) ---> " + ("0.0.0.0".matches(IP))); 292 } 293 294 295 296}