chenjiahe
2022-07-12 cd5e48b7aa68c555e77402832bc84e87a47853ad
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 /**
7  * 正则表达式验证工�?
8  * @author mgchen
9  *
10  */
11 public class RegValidatorUtil {
12     
13     /**
14     * 验证邮箱
15     * 
16     * @param str 待验证的字符
17     * @return 如果是符合的字符�?,返回 <b>true </b>,否则�? <b>false </b>
18     */
19     public static boolean isEmail(String str) {
20         String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
21         return match(regex, str);
22     }
23
24     /**
25     * 验证IP地址
26     * 
27     * @param str 待验证的字符�?
28     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
29     */
30     public static boolean isIP(String str) {
31         String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
32         String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
33         return match(regex, str);
34     }
35
36     /**
37     * 验证网址Url
38     * 
39     * @param str 待验证的字符�?
40     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
41     */
42     public static boolean IsUrl(String str) {
43         String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
44         return match(regex, str);
45     }
46
47     /**
48     * 验证电话号码
49     * 
50     * @param str 待验证的字符�?
51     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
52     */
53     public static boolean IsTelephone(String str) {
54         String regex = "^(\\d{3,4}-)?\\d{6,8}$";
55         return match(regex, str);
56     }
57
58     /**
59     * 验证输入密码条件(字符与数据同时出�?)
60     * 
61     * @param str 待验证的字符�?
62     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
63     */
64     public static boolean IsPassword(String str) {
65         String regex = "[A-Za-z0-9]+";
66         return match(regex, str);
67     }
68
69     /**
70     * 验证输入密码长度 (6-18�?)
71     * 
72     * @param str 待验证的字符�?
73     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
74     */
75     public static boolean IsPasswLength(String str) {
76         String regex = "^\\d{6,18}$";
77         return match(regex, str);
78     }
79
80     /**
81     * 验证输入邮政编号
82     * 
83     * @param str 待验证的字符�?
84     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
85     */
86     public static boolean IsPostalcode(String str) {
87         String regex = "^\\d{6}$";
88         return match(regex, str);
89     }
90
91     /**
92     * 验证输入手机号码
93     * 
94     * @param str 待验证的字符�?
95     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
96     */
97     public static boolean IsHandset(String str) {
98         String regex = "^[1]+[3,5]+\\d{9}$";
99         return match(regex, str);
100     }
101
102     /**
103     * 验证输入身份证号
104     * 
105     * @param str 待验证的字符�?
106     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
107     */
108     public static boolean IsIDcard(String str) {
109         String regex = "(^\\d{18}$)|(^\\d{15}$)";
110         return match(regex, str);
111     }
112
113     /**
114     * 验证输入两位小数
115     * 
116     * @param str 待验证的字符�?
117     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
118     */
119     public static boolean IsDecimal(String str) {
120         String regex = "^[0-9]+(.[0-9]{2})?$";
121         return match(regex, str);
122     }
123
124     /**
125     * 验证输入�?年的12个月
126     * 
127     * @param str 待验证的字符�?
128     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
129     */
130     public static boolean IsMonth(String str) {
131         String regex = "^(0?[[1-9]|1[0-2])$";
132         return match(regex, str);
133     }
134
135     /**
136     * 验证输入�?个月�?31�?
137     * 
138     * @param str 待验证的字符�?
139     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
140     */
141     public static boolean IsDay(String str) {
142         String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
143         return match(regex, str);
144     }
145
146     /**
147     * 验证日期时间
148     * 
149     * @param str 待验证的字符�?
150     * @return 如果是符合网�?格式的字符串,返回 <b>true </b>,否则�? <b>false </b>
151     */
152     public static boolean isDate(String str) {
153         // 严格验证时间格式�?(匹配[2002-01-31], [1997-04-30],
154         // [2004-01-01])不匹�?([2002-01-32], [2003-02-29], [04-01-01])
155         // String regex =
156         // "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$";
157         // 没加时间验证的YYYY-MM-DD
158         // String regex =
159         // "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
160         // 加了时间验证的YYYY-MM-DD 00:00:00
161         String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$";
162         return match(regex, str);
163     }
164
165     /**
166     * 验证数字输入
167     * 
168     * @param str 待验证的字符�?
169     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
170     */
171     public static boolean IsNumber(String str) {
172         String regex = "^[0-9]*$";
173         return match(regex, str);
174     }
175
176     /**
177     * 验证非零的正整数
178     * 
179     * @param str 待验证的字符�?
180     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
181     */
182     public static boolean IsIntNumber(String str) {
183         String regex = "^[1-9][0-9]*$";
184         return match(regex, str);
185     }
186
187     /**
188     * 验证大写字母
189     * 
190     * @param str 待验证的字符�?
191     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
192     */
193     public static boolean IsUpChar(String str) {
194         String regex = "^[A-Z]+$";
195         return match(regex, str);
196     }
197
198     /**
199     * 验证小写字母
200     * 
201     * @param str 待验证的字符�?
202     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
203     */
204     public static boolean IsLowChar(String str) {
205         String regex = "^[a-z]+$";
206         return match(regex, str);
207     }
208
209     /**
210     * 验证验证输入字母
211     * 
212     * @param str 待验证的字符�?
213     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
214     */
215     public static boolean IsLetter(String str) {
216         String regex = "^[A-Za-z]+$";
217         return match(regex, str);
218     }
219
220     /**
221     * 验证验证输入汉字
222     * 
223     * @param str 待验证的字符�?
224     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
225     */
226     public static boolean IsChinese(String str) {
227         String regex = "^[\u4e00-\u9fa5],{0,}$";
228         return match(regex, str);
229     }
230
231     /**
232     * 验证验证输入字符�?
233     * 
234     * @param str 待验证的字符�?
235     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
236     */
237     public static boolean IsLength(String str) {
238         String regex = "^.{8,}$";
239         return match(regex, str);
240     }
241
242     /**
0e1429 243      * 判断字符串是不是double型
E 244      * @param str
245      * @return
246      */
247     public static boolean isNumeric(String str){
248         Pattern pattern = Pattern.compile("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}");
249         Matcher isNum = pattern.matcher(str);
250         if( !isNum.matches() ){
251             return false;
252         }
253         return true;
254     }
255
256     /**
5c5945 257     * @param regex
E 258     * 正则表达式字符串
259     * @param str
260     * 要匹配的字符�?
261     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
262     */
263     private static boolean match(String regex, String str) {
264         Pattern pattern = Pattern.compile(regex);
265         Matcher matcher = pattern.matcher(str);
266         return matcher.matches();
267     }
268 }