正则表达式类

这篇文章主要讲解 正则表达式类

这个类包含了项目中绝大多数要使用的正则表达式,具体看代码:

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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package org.xxz.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Jarvis
* 90%的验证都调用了Regular方法 但是本类也可删除大部分方法 涉及到正则的判断都直接穿参数和正则表达式
* 但是为了方便业务类调用和有更直观的含义 建议不要这么做
* Pattern的matcher已经被同步synchronized 所以 此类的任何使用正则验证的方法都不需要同步
*
*/
public class validate {
public validate()
{
}
/* ------------------常量定义 */
/**
* Email正则表达式=^/w+([-+.]/w+)*@/w+([-.]/w+)*/./ w + ([-.] / w +) * $
* /
public static final String EMAIL = "^//w+([-+.]//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*$";
/**
* 电话号码正则表达式= (^(/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}$)";
/**
* 手机号码正则表达式=^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])/d{8}$
*/
public static final String MOBILE = "^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])//d{8}$";
/**
* IP地址正则表达式 ((?:(?:25[0-5]|2[0-4]//d|[01]?//d?//d)//.){3}(?:25[0-5]|2[0-4]//d|[01]?//d?//d))
*/
public static final String IPADDRESS = "((?:(?:25[0-5]|2[0-4]//d|[01]?//d?//d)//.){3}(?:25[0-5]|2[0-4]//d|[01]?//d?//d))";
/**
* Integer正则表达式 ^-?(([1-9]/d*$)|0)
*/
public static final String INTEGER = "^-?(([1-9]//d*$)|0)";
/**
* 正整数正则表达式 >=0 ^[1-9]/d*|0$
*/
public static final String INTEGER_NEGATIVE = "^[1-9]//d*|0$";
/**
* 负整数正则表达式 <=0 ^-[1-9]/d*|0$
*/
public static final String INTEGER_POSITIVE = "^-[1-9]//d*|0$";
/**
* Double正则表达式 ^-?([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正则表达式 >=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$";
/**
* 负Double正则表达式 <= 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$";
/**
* 年龄正则表达式 ^(?:[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)$";
/**
* 邮编正则表达式 [1-9]/d{5}(?!/d) 国内6位邮编
*/
public static final String CODE = "[1-9]//d{5}(?!//d)";
/**
* 匹配由数字、26个英文字母或者下划线组成的字符串 ^/w+$
*/
public static final String STR_ENG_NUM_ = "^//w+$";
/**
* 匹配由数字和26个英文字母组成的字符串 ^[A-Za-z0-9]+$
*/
public static final String STR_ENG_NUM = "^//w+$";
/**
* 匹配由26个英文字母组成的字符串 ^[A-Za-z]+$
*/
public static final String STR_ENG = "^[A-Za-z]+$";
/**
* 匹配中文字符 ^[/u0391-/uFFE5]+$
*/
public static final String STR_CHINA = "^[/u0391-/uFFE5]+$";
/**
* 过滤特殊字符串正则
* regEx="[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
*/
public static final String STR_SPECIAL = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
/**
* *只能输英文 数字 中文 ^[a-zA-Z0-9/u4e00-/u9fa5]+$
*/
public static final String STR_ENG_CHA_NUM = "^[a-zA-Z0-9/u4e00-/u9fa5]+$";
/**
*
*/
/***
* 日期正则 支持:
* 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)$))";
/**
* URL正则表达式
* 匹配 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*)*)$";
/**
* 身份证正则表达式
*/
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}))";
/**
* 1.匹配科学计数 e或者E必须出现有且只有一次 不含Dd
* 正则 ^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))$
*/
public final static String SCIENTIFIC_A = "^[-+]?(//d+(//.//d*)?|//.//d+)([eE]([-+]?([012]?//d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))$";
/**
* 2.匹配科学计数 e或者E必须出现有且只有一次 结尾包含Dd
* 正则 ^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))[dD]?$
*/
public final static String SCIENTIFIC_B = "^[-+]?(//d+(//.//d*)?|//.//d+)([eE]([-+]?([012]?//d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))[dD]?$";
/**
* 3.匹配科学计数 是否含有E或者e都通过 结尾含有Dd的也通过(针对Double类型)
* 正则 ^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?[dD]?$
*/
public final static String SCIENTIFIC_C = "^[-+]?(//d+(//.//d*)?|//.//d+)([eE]([-+]?([012]?//d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?[dD]?$";
/**
* 4.匹配科学计数 是否含有E或者e都通过 结尾不含Dd
* 正则 ^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?$
*/
public final static String SCIENTIFIC_D = "^[-+]?(//d+(//.//d*)?|//.//d+)([eE]([-+]?([012]?//d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?$";
/* //------------------验证方法 */
/**
* 判断字段是否为空 符合返回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转空
* @param str
* @return boolean
*/
public static String nulltoStr( String str )
{
return(StrisNull( str ) ? "" : str);
}
/**
* 字符串null赋值默认值
* @param str 目标字符串
* @param defaut 默认值
* @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 ) );
}
/**
* 判断是否为电话号码 符合返回ture
* @param str
* @return boolean
*/
public static boolean isPhone( String str )
{
return(Regular( str, PHONE ) );
}
/**
* 判断是否为手机号码 符合返回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 ) );
}
/**
* 判断是否为IP地址 符合返回ture
* @param str
* @return boolean
*/
public static boolean isIpaddress( String str )
{
return(Regular( str, IPADDRESS ) );
}
/**
* 判断字段是否为数字 正负整数 正负浮点数 符合返回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 ) );
}
/**
* 判断字段是否为正整数正则表达式 >=0 符合返回ture
* @param str
* @return boolean
*/
public static boolean isINTEGER_NEGATIVE( String str )
{
return(Regular( str, INTEGER_NEGATIVE ) );
}
/**
* 判断字段是否为负整数正则表达式 <=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 ) );
}
/**
* 判断字段是否为正浮点数正则表达式 >=0 符合返回ture
* @param str
* @return boolean
*/
public static boolean isDOUBLE_NEGATIVE( String str )
{
return(Regular( str, DOUBLE_NEGATIVE ) );
}
/**
* 判断字段是否为负浮点数正则表达式 <=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 ) );
}
/**
* 判断字段是否为年龄 符合返回ture
* @param str
* @return boolean
*/
public static boolean isAge( String str )
{
return(Regular( str, AGE ) );
}
/**
* 判断字段是否超长
* 字串为空返回fasle, 超过长度{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 isChina( String str )
{
return(Regular( str, STR_CHINA ) );
}
/**
* 判断字符串是不是全部是英文字母
* @param str
* @return boolean
*/
public static boolean isEnglish( String str )
{
return(Regular( str, STR_ENG ) );
}
/**
* 判断字符串是不是全部是英文字母+数字
* @param str
* @return boolean
*/
public static boolean isENG_NUM( String str )
{
return(Regular( str, STR_ENG_NUM ) );
}
/**
* 判断字符串是不是全部是英文字母+数字+下划线
* @param str
* @return boolean
*/
public static boolean isENG_NUM_( String str )
{
return(Regular( str, STR_ENG_NUM_ ) );
}
/**
* 过滤特殊字符串 返回过滤后的字符串
* @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() );
}
/**
* 匹配是否符合正则表达式pattern 匹配返回true
* @param str 匹配的字符串
* @param pattern 匹配模式
* @return boolean
*/
private static boolean Regular( String str, String pattern )
{
System.out.println( "pattern=" + pattern );
if ( null == str || str.trim().length() <= 0 )
return(false);
Pattern p = Pattern.compile( pattern );
Matcher m = p.matcher( str );
return(m.matches() );
}
/**
* 判断是不是科学计数法 如果是 返回true
* 匹配科学计数 e或者E必须出现有且只有一次 结尾不含D
* 匹配模式可参考本类定义的 SCIENTIFIC_A,SCIENTIFIC_B,SCIENTIFIC_C,SCIENTIFIC_D
* 若判断为其他模式可调用 Regular(String str,String pattern)方法
* @param str 科学计数字符串
* @return boolean
*/
public static boolean isScientific( String str )
{
if ( StrisNull( str ) )
return(false);
return(Regular( str, validate.SCIENTIFIC_A ) );
}
}