正則表達(dá)式常常用來(lái)驗(yàn)證各種表單,Java 表單注冊(cè)常用正則表達(dá)式驗(yàn)證工具類,常用正則表達(dá)式大集合。
1. 電話號(hào)碼
2. 郵編
3. QQ
4. E-mail
5. 手機(jī)號(hào)碼
6. URL
7. 是否為數(shù)字
8. 是否為中文
9. 身份證
10. 域名
11. IP 。。。。
常用驗(yàn)證應(yīng)有盡有! 這的確是您從事 web 開(kāi)發(fā),服務(wù)器端表單驗(yàn)證之良品!你,值得擁有 ^_^
1
2
3
4
5
6
7
8
9
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* * Copyright 2012-2013 The Haohui Network Corporation */ package com.haohui.common.utils; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @project baidamei * @author cevencheng <cevencheng@gmail.com> * @create 2012-11-15 下午4:54:42 */ public class RegexUtils { /** * 驗(yàn)證Email * @param email email地址,格式:zhangsan@zuidaima.com,zhangsan@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)碼段 *<p>移動(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專用)</p> *<p>聯(lián)通的號(hào)段:130、131、132、155、156(世界風(fēng)專用)、185(未啟用)、186(3g)</p> *<p>電信的號(hào)段:133、153、180(未啟用)、189</p> * @return 驗(yàn)證成功返回true,驗(yàn)證失敗返回false */ public static boolean checkMobile(String mobile) { String regex = "(\\+\\d+)?1[34578]\\d{9}$"; return Pattern.matches(regex,mobile); } /** * 驗(yàn)證固定電話號(hào)碼 * @param phone 電話號(hào)碼,格式:國(guó)家(地區(qū))電話代碼 + 區(qū)號(hào)(城市代碼) + 電話號(hào)碼,如:+8602085588447 * <p><b>國(guó)家(地區(qū)) 代碼 :</b>標(biāo)識(shí)電話號(hào)碼的國(guó)家(地區(qū))的標(biāo)準(zhǔn)國(guó)家(地區(qū))代碼。它包含從 0 到 9 的一位或多位數(shù)字, * 數(shù)字之后是空格分隔的國(guó)家(地區(qū))代碼。</p> * <p><b>區(qū)號(hào)(城市代碼):</b>這可能包含一個(gè)或多個(gè)從 0 到 9 的數(shù)字,地區(qū)或城市代碼放在圓括號(hào)—— * 對(duì)不使用地區(qū)或城市代碼的國(guó)家(地區(qū)),則省略該組件。</p> * <p><b>電話號(hào)碼:</b>這包含從 0 到 9 的一個(gè)或多個(gè)數(shù)字 </p> * @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); } /** * <pre> * 獲取網(wǎng)址 URL 的一級(jí)域 * </pre> * * @param url * @return */ public static String getDomain(String url) { Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE); // 獲取完整的域名 // 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); } } |
分享一個(gè)用正則表達(dá)式校驗(yàn)電話號(hào)碼、身份證號(hào)、日期格式、URL、Email等等格式的工具類
1
2
3
4
5
6
7
8
9
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
package com.eabax.util; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 驗(yàn)證工具類 * @author admin * */ public class Validation { //------------------常量定義 /** * Email正則表達(dá)式="^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; */ //public static final String EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";; public static final String EMAIL = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+" ; /** * 電話號(hào)碼正則表達(dá)式= (^(\d{2,4}[-_-—]?)?\d{3,8}([-_-—]?\d{3,8})?([-_-—]?\d{1,7})?$)|(^0?1[35]\d{9}$) */ public static final String PHONE = "(^(\\d{2,4}[-_-—]?)?\\d{3,8}([-_-—]?\\d{3,8})?([-_-—]?\\d{1,7})?$)|(^0?1[35]\\d{9}$)" ; /** * 手機(jī)號(hào)碼正則表達(dá)式=^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$ */ public static final String MOBILE = "^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\\d{8}$" ; /** * Integer正則表達(dá)式 ^-?(([1-9]\d*$)|0) */ public static final String INTEGER = "^-?(([1-9]\\d*$)|0)" ; /** * 正整數(shù)正則表達(dá)式 >=0 ^[1-9]\d*|0$ */ public static final String INTEGER_NEGATIVE = "^[1-9]\\d*|0$" ; /** * 負(fù)整數(shù)正則表達(dá)式 <=0 ^-[1-9]\d*|0$ */ public static final String INTEGER_POSITIVE = "^-[1-9]\\d*|0$" ; /** * Double正則表達(dá)式 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ */ public static final String DOUBLE = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$" ; /** * 正Double正則表達(dá)式 >=0 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ */ public static final String DOUBLE_NEGATIVE = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$" ; /** * 負(fù)Double正則表達(dá)式 <= 0 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ */ public static final String DOUBLE_POSITIVE = "^(-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*))|0?\\.0+|0$" ; /** * 年齡正則表達(dá)式 ^(?:[1-9][0-9]?|1[01][0-9]|120)$ 匹配0-120歲 */ public static final String AGE= "^(?:[1-9][0-9]?|1[01][0-9]|120)$" ; /** * 郵編正則表達(dá)式 [0-9]\d{5}(?!\d) 國(guó)內(nèi)6位郵編 */ public static final String CODE= "[0-9]\\d{5}(?!\\d)" ; /** * 匹配由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串 ^\w+$ */ public static final String STR_ENG_NUM_= "^\\w+$" ; /** * 匹配由數(shù)字和26個(gè)英文字母組成的字符串 ^[A-Za-z0-9]+$ */ public static final String STR_ENG_NUM= "^[A-Za-z0-9]+" ; /** * 匹配由26個(gè)英文字母組成的字符串 ^[A-Za-z]+$ */ public static final String STR_ENG= "^[A-Za-z]+$" ; /** * 過(guò)濾特殊字符串正則 * regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]"; */ public static final String STR_SPECIAL= "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]" ; /*** * 日期正則 支持: * YYYY-MM-DD * YYYY/MM/DD * YYYY_MM_DD * YYYYMMDD * YYYY.MM.DD的形式 */ public static final String DATE_ALL= "((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(10|12|0?[13578])([-\\/\\._]?)(3[01]|[12][0-9]|0?[1-9])$)" + "|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(11|0?[469])([-\\/\\._]?)(30|[12][0-9]|0?[1-9])$)" + "|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(0?2)([-\\/\\._]?)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([3579][26]00)" + "([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)" + "|(^([1][89][0][48])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][0][48])([-\\/\\._]?)" + "(0?2)([-\\/\\._]?)(29)$)" + "|(^([1][89][2468][048])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._]?)(0?2)" + "([-\\/\\._]?)(29)$)|(^([1][89][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|" + "(^([2-9][0-9][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$))" ; /*** * 日期正則 支持: * YYYY-MM-DD */ public static final String DATE_FORMAT1= "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)" ; /** * URL正則表達(dá)式 * 匹配 http www ftp */ public static final String URL = "^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?" + "(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*" + "(\\w*:)*(\\w*\\+)*(\\w*\\.)*" + "(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$" ; /** * 身份證正則表達(dá)式 */ public static final String IDCARD= "((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][0-9][0-9]{3}" + "[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))" ; /** * 機(jī)構(gòu)代碼 */ public static final String JIGOU_CODE = "^[A-Z0-9]{8}-[A-Z0-9]$" ; /** * 匹配數(shù)字組成的字符串 ^[0-9]+$ */ public static final String STR_NUM = "^[0-9]+$" ; ////------------------驗(yàn)證方法 /** * 判斷字段是否為空 符合返回ture * @param str * @return boolean */ public static synchronized boolean StrisNull(String str) { return null == str || str.trim().length() <= 0 ? true : false ; } /** * 判斷字段是非空 符合返回ture * @param str * @return boolean */ public static boolean StrNotNull(String str) { return !StrisNull(str) ; } /** * 字符串null轉(zhuǎn)空 * @param str * @return boolean */ public static String nulltoStr(String str) { return StrisNull(str)? "" :str; } /** * 字符串null賦值默認(rèn)值 * @param str 目標(biāo)字符串 * @param defaut 默認(rèn)值 * @return String */ public static String nulltoStr(String str,String defaut) { return StrisNull(str)?defaut:str; } /** * 判斷字段是否為Email 符合返回ture * @param str * @return boolean */ public static boolean isEmail(String str) { return Regular(str,EMAIL); } /** * 判斷是否為電話號(hào)碼 符合返回ture * @param str * @return boolean */ public static boolean isPhone(String str) { return Regular(str,PHONE); } /** * 判斷是否為手機(jī)號(hào)碼 符合返回ture * @param str * @return boolean */ public static boolean isMobile(String str) { return Regular(str,MOBILE); } /** * 判斷是否為Url 符合返回ture * @param str * @return boolean */ public static boolean isUrl(String str) { return Regular(str,URL); } /** * 判斷字段是否為數(shù)字 正負(fù)整數(shù) 正負(fù)浮點(diǎn)數(shù) 符合返回ture * @param str * @return boolean */ public static boolean isNumber(String str) { return Regular(str,DOUBLE); } /** * 判斷字段是否為INTEGER 符合返回ture * @param str * @return boolean */ public static boolean isInteger(String str) { return Regular(str,INTEGER); } /** * 判斷字段是否為正整數(shù)正則表達(dá)式 >=0 符合返回ture * @param str * @return boolean */ public static boolean isINTEGER_NEGATIVE(String str) { return Regular(str,INTEGER_NEGATIVE); } /** * 判斷字段是否為負(fù)整數(shù)正則表達(dá)式 <=0 符合返回ture * @param str * @return boolean */ public static boolean isINTEGER_POSITIVE(String str) { return Regular(str,INTEGER_POSITIVE); } /** * 判斷字段是否為DOUBLE 符合返回ture * @param str * @return boolean */ public static boolean isDouble(String str) { return Regular(str,DOUBLE); } /** * 判斷字段是否為正浮點(diǎn)數(shù)正則表達(dá)式 >=0 符合返回ture * @param str * @return boolean */ public static boolean isDOUBLE_NEGATIVE(String str) { return Regular(str,DOUBLE_NEGATIVE); } /** * 判斷字段是否為負(fù)浮點(diǎn)數(shù)正則表達(dá)式 <=0 符合返回ture * @param str * @return boolean */ public static boolean isDOUBLE_POSITIVE(String str) { return Regular(str,DOUBLE_POSITIVE); } /** * 判斷字段是否為日期 符合返回ture * @param str * @return boolean */ public static boolean isDate(String str) { return Regular(str,DATE_ALL); } /** * 驗(yàn)證2010-12-10 * @param str * @return */ public static boolean isDate1(String str) { return Regular(str,DATE_FORMAT1); } /** * 判斷字段是否為年齡 符合返回ture * @param str * @return boolean */ public static boolean isAge(String str) { return Regular(str,AGE) ; } /** * 判斷字段是否超長(zhǎng) * 字串為空返回fasle, 超過(guò)長(zhǎng)度{leng}返回ture 反之返回false * @param str * @param leng * @return boolean */ public static boolean isLengOut(String str, int leng) { return StrisNull(str)? false :str.trim().length() > leng ; } /** * 判斷字段是否為身份證 符合返回ture * @param str * @return boolean */ public static boolean isIdCard(String str) { if (StrisNull(str)) return false ; if (str.trim().length() == 15 || str.trim().length() == 18 ) { return Regular(str,IDCARD); } else { return false ; } } /** * 判斷字段是否為郵編 符合返回ture * @param str * @return boolean */ public static boolean isCode(String str) { return Regular(str,CODE) ; } /** * 判斷字符串是不是全部是英文字母 * @param str * @return boolean */ public static boolean isEnglish(String str) { return Regular(str,STR_ENG) ; } /** * 判斷字符串是不是全部是英文字母+數(shù)字 * @param str * @return boolean */ public static boolean isENG_NUM(String str) { return Regular(str,STR_ENG_NUM) ; } /** * 判斷字符串是不是全部是英文字母+數(shù)字+下劃線 * @param str * @return boolean */ public static boolean isENG_NUM_(String str) { return Regular(str,STR_ENG_NUM_) ; } /** * 過(guò)濾特殊字符串 返回過(guò)濾后的字符串 * @param str * @return boolean */ public static String filterStr(String str) { Pattern p = Pattern.compile(STR_SPECIAL); Matcher m = p.matcher(str); return m.replaceAll( "" ).trim(); } /** * 校驗(yàn)機(jī)構(gòu)代碼格式 * @return */ public static boolean isJigouCode(String str){ return Regular(str,JIGOU_CODE) ; } /** * 判斷字符串是不是數(shù)字組成 * @param str * @return boolean */ public static boolean isSTR_NUM(String str) { return Regular(str,STR_NUM) ; } /** * 匹配是否符合正則表達(dá)式pattern 匹配返回true * @param str 匹配的字符串 * @param pattern 匹配模式 * @return boolean */ private static boolean Regular(String str,String pattern){ if ( null == str || str.trim().length()<= 0 ) return false ; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(str); return m.matches(); } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。