正则工具

正则工具

  1 /**
  2  * <html>
  3  * <body>
  4  *  <P> Copyright 1994 JsonInternational</p>
  5  *  <p> All rights reserved.</p>
  6  *  <p> Created on 19941115</p>
  7  *  <p> Created by Jason</p>
  8  *  </body>
  9  * </html>
 10  */
 11 package cn.ucaner.alpaca.framework.utils.regex;
 12 
 13 import java.util.Collection;
 14 import java.util.Map;
 15 import java.util.regex.Matcher;
 16 import java.util.regex.Pattern;
 17 
 18 /**
 19 * @Package:cn.ucaner.framework.utils   
 20 * @ClassName:RegexUtil   
 21 * @Description:   <p> 正则工具</p>
 22 * @Author: - Jason 
 23 * @CreatTime:2017年8月30日 下午2:08:11   
 24 * @Modify By:   
 25 * @ModifyTime:  
 26 * @Modify marker:   
 27 * @version    V1.0
 28  */
 29 public class RegexUtil {
 30 
 31     public static void main(String[] args) {
 32 
 33     }
 34 
 35     public final static boolean isNull(Object[] objs) {
 36         if (objs == null || objs.length == 0) {
 37             return true;
 38         }
 39         return false;
 40     }
 41 
 42     public final static boolean isNull(Integer integer) {
 43         if (integer == null || integer == 0) {
 44             return true;
 45         }
 46         return false;
 47     }
 48 
 49     public final static boolean isNull(Collection<?> collection) {
 50         if (collection == null || collection.size() == 0) {
 51             return true;
 52         }
 53         return false;
 54     }
 55 
 56     public final static boolean isNull(Map<?, ?> map) {
 57         if (map == null || map.size() == 0) {
 58             return true;
 59         }
 60         return false;
 61     }
 62 
 63     public final static boolean isNull(String str) {
 64         return str == null || "".equals(str.trim()) || "null".equals(str.toLowerCase());
 65     }
 66 
 67     public final static boolean isNull(Long longs) {
 68         if (longs == null || longs == 0) {
 69             return true;
 70         }
 71         return false;
 72     }
 73 
 74     public final static boolean isNotNull(Long longs) {
 75         return !isNull(longs);
 76     }
 77 
 78     public final static boolean isNotNull(String str) {
 79         return !isNull(str);
 80     }
 81 
 82     public final static boolean isNotNull(Collection<?> collection) {
 83         return !isNull(collection);
 84     }
 85 
 86     public final static boolean isNotNull(Map<?, ?> map) {
 87         return !isNull(map);
 88     }
 89 
 90     public final static boolean isNotNull(Integer integer) {
 91         return !isNull(integer);
 92     }
 93 
 94     public final static boolean isNotNull(Object[] objs) {
 95         return !isNull(objs);
 96     }
 97 
 98     /**
 99      * 匹配URL地址
100      * @param str
101      * @return
102      */
103     public final static boolean isUrl(String str) {
104         return match(str, "^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$");
105     }
106 
107     /**
108      * 匹配密码,以字母开头,长度在6-12之间,只能包含字符、数字和下划线。
109      * @param str
110      * @return
111      */
112     public final static boolean isPwd(String str) {
113         return match(str, "^[a-zA-Z]\\w{6,12}$");
114     }
115 
116     /**
117      * 验证字符,只能包含中文、英文、数字、下划线等字符。
118      * @param str
119      * @return
120      */
121     public final static boolean stringCheck(String str) {
122         return match(str, "^[a-zA-Z0-9\u4e00-\u9fa5-_]+$");
123     }
124 
125     /**
126      * 匹配Email地址
127      * @param str
128      * @return
129      */
130     public final static boolean isEmail(String str) {
131         return match(str, "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
132     }
133 
134     /**
135      * 匹配非负整数(正整数+0)
136      * @param str
137      * @return
138      */
139     public final static boolean isInteger(String str) {
140         return match(str, "^[+]?\\d+$");
141     }
142 
143     /**
144      * 判断数值类型,包括整数和浮点数
145      * @param str
146      * @return
147      */
148     public final static boolean isNumeric(String str) {
149         if (isFloat(str) || isInteger(str)) {
150             return true;
151         }
152         return false;
153     }
154 
155     /**
156      * 只能输入数字
157      * @param str
158      * @return
159      */
160     public final static boolean isDigits(String str) {
161         return match(str, "^[0-9]*$");
162     }
163 
164     /**
165      * 匹配正浮点数
166      * @param str
167      * @return
168      */
169     public final static boolean isFloat(String str) {
170         return match(str, "^[-\\+]?\\d+(\\.\\d+)?$");
171     }
172 
173     /**
174      * 联系电话(手机/电话皆可)验证
175      * @param text
176      * @return
177      */
178     public final static boolean isTel(String text) {
179         if (isMobile(text) || isPhone(text)) {
180             return true;
181         }
182         return false;
183     }
184 
185     /**
186      * 电话号码验证
187      * @param text
188      * @return
189      */
190     public final static boolean isPhone(String text) {
191         return match(text, "^(\\d{3,4}-?)?\\d{7,9}$");
192     }
193 
194     /**
195      * 手机号码验证
196      * @param text
197      * @return
198      */
199     public final static boolean isMobile(String text) {
200         if (text.length() != 11) {
201             return false;
202         }
203         return match(text, "^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\\d{8})$");
204     }
205 
206     /**
207      * 身份证号码验证
208      * @param text
209      * @return
210      */
211     public final static boolean isIdCardNo(String text) {
212         return match(text, "^(\\d{6})()?(\\d{4})(\\d{2})(\\d{2})(\\d{3})(\\w)$");
213     }
214 
215     /**
216      * 邮政编码验证
217      * @param text
218      * @return
219      */
220     public final static boolean isZipCode(String text) {
221         return match(text, "^[0-9]{6}$");
222     }
223 
224     /**
225      * 判断整数num是否等于0
226      * @param num
227      * @return
228      */
229     public final static boolean isIntEqZero(int num) {
230         return num == 0;
231     }
232 
233     /**
234      * 判断整数num是否大于0
235      * @param num
236      * @return
237      */
238     public final static boolean isIntGtZero(int num) {
239         return num > 0;
240     }
241 
242     /**
243      * 判断整数num是否大于或等于0
244      * @param num
245      * @return
246      */
247     public final static boolean isIntGteZero(int num) {
248         return num >= 0;
249     }
250 
251     /**
252      * 判断浮点数num是否等于0
253      * @param num  浮点数
254      * @return
255      */
256     public final static boolean isFloatEqZero(float num) {
257         return num == 0f;
258     }
259 
260     /**
261      * 判断浮点数num是否大于0
262      * @param num 浮点数
263      * @return
264      */
265     public final static boolean isFloatGtZero(float num) {
266         return num > 0f;
267     }
268 
269     /**
270      * 判断浮点数num是否大于或等于0
271      * @param num 浮点数
272      * @return
273      */
274     public final static boolean isFloatGteZero(float num) {
275         return num >= 0f;
276     }
277 
278     /**
279      * 判断是否为合法字符(a-zA-Z0-9-_)
280      * @param text
281      * @return
282      */
283     public final static boolean isRightfulString(String text) {
284         return match(text, "^[A-Za-z0-9_-]+$");
285     }
286 
287     /**
288      * 判断英文字符(a-zA-Z)
289      * @param text
290      * @return
291      */
292     public final static boolean isEnglish(String text) {
293         return match(text, "^[A-Za-z]+$");
294     }
295 
296     /**
297      * 判断中文字符(包括汉字和符号)
298      * @param text
299      * @return
300      */
301     public final static boolean isChineseChar(String text) {
302         return match(text, "^[\u0391-\uFFE5]+$");
303     }
304 
305     /**
306      * 匹配汉字
307      * @param text
308      * @return
309      */
310     public final static boolean isChinese(String text) {
311         return match(text, "^[\u4e00-\u9fa5]+$");
312     }
313 
314     /**
315      * 是否包含中英文特殊字符,除英文"-_"字符外
316      * @param str
317      * @return
318      */
319     public static boolean isContainsSpecialChar(String text) {
320         if (org.apache.commons.lang3.StringUtils.isBlank(text)) {
321             return false;
322         }
323         String[] chars = { "[", "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "+", "=", "|", "{", "}", "'", ":", ";", "'", ",", "[",
324                 "]", ".", "<", ">", "/", "?", "~", "!", "@", "#", "¥", "%", "…", "&", "*", "(", ")", "—", "+", "|", "{", "}", "【", "】", "‘", ";",
325                 ":", "”", "“", "’", "。", ",", "、", "?", "]" };
326         for (String ch : chars) {
327             if (text.contains(ch)) {
328                 return true;
329             }
330         }
331         return false;
332     }
333 
334     /**
335      * 过滤中英文特殊字符,除英文"-_"字符外
336      * @param text
337      * @return
338      */
339     public static String stringFilter(String text) {
340         String regExpr = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
341         Pattern p = Pattern.compile(regExpr);
342         Matcher m = p.matcher(text);
343         return m.replaceAll("").trim();
344     }
345 
346     /**
347      * 过滤html代码
348      * @param inputString 含html标签的字符串
349      * @return
350      */
351     public static String htmlFilter(String inputString) {
352         String htmlStr = inputString; // 含html标签的字符串
353         String textStr = "";
354         java.util.regex.Pattern p_script;
355         java.util.regex.Matcher m_script;
356         java.util.regex.Pattern p_style;
357         java.util.regex.Matcher m_style;
358         java.util.regex.Pattern p_html;
359         java.util.regex.Matcher m_html;
360         java.util.regex.Pattern p_ba;
361         java.util.regex.Matcher m_ba;
362 
363         try {
364             String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
365             // }
366             String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
367             // }
368             String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
369             String patternStr = "\\s+";
370 
371             p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
372             m_script = p_script.matcher(htmlStr);
373             htmlStr = m_script.replaceAll(""); // 过滤script标签
374 
375             p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
376             m_style = p_style.matcher(htmlStr);
377             htmlStr = m_style.replaceAll(""); // 过滤style标签
378 
379             p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
380             m_html = p_html.matcher(htmlStr);
381             htmlStr = m_html.replaceAll(""); // 过滤html标签
382 
383             p_ba = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
384             m_ba = p_ba.matcher(htmlStr);
385             htmlStr = m_ba.replaceAll(""); // 过滤空格
386 
387             textStr = htmlStr;
388 
389         } catch (Exception e) {
390             System.err.println("Html2Text: " + e.getMessage());
391         }
392         // 返回文本字符串
393         return textStr;
394     }
395 
396     /**
397      * 正则表达式匹配
398      * @param text 待匹配的文本
399      * @param reg 正则表达式
400      * @return
401      */
402     private final static boolean match(String text, String reg) {
403         if (org.apache.commons.lang3.StringUtils.isBlank(text) || org.apache.commons.lang3.StringUtils.isBlank(reg)) {
404             return false;
405         }
406         return Pattern.compile(reg).matcher(text).matches();
407     }
408     // 附 : 常用的正则表达式:
409     // 匹配特定数字:
410     // ^[1-9]d*$    //匹配正整数
411     // ^-[1-9]d*$   //匹配负整数
412     // ^-?[1-9]d*$   //匹配整数
413     // ^[1-9]d*|0$  //匹配非负整数(正整数 + 0)
414     // ^-[1-9]d*|0$   //匹配非正整数(负整数 + 0)
415     // ^[1-9]d*.d*|0.d*[1-9]d*$   //匹配正浮点数
416     // ^-([1-9]d*.d*|0.d*[1-9]d*)$  //匹配负浮点数
417     // ^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$  //匹配浮点数
418     // ^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$   //匹配非负浮点数(正浮点数 + 0)
419     // ^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$  //匹配非正浮点数(负浮点数 + 0)
420     // 评注:处理大量数据时有用,具体应用时注意修正
421     //
422     // 匹配特定字符串:
423     // ^[A-Za-z]+$  //匹配由26个英文字母组成的字符串
424     // ^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串
425     // ^[a-z]+$  //匹配由26个英文字母的小写组成的字符串
426     // ^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串
427     // ^w+$  //匹配由数字、26个英文字母或者下划线组成的字符串
428     //
429     // 在使用RegularExpressionValidator验证控件时的验证功能及其验证表达式介绍如下:
430     //
431     // 只能输入数字:“^[0-9]*$”
432     // 只能输入n位的数字:“^d{n}$”
433     // 只能输入至少n位数字:“^d{n,}$”
434     // 只能输入m-n位的数字:“^d{m,n}$”
435     // 只能输入零和非零开头的数字:“^(0|[1-9][0-9]*)$”
436     // 只能输入有两位小数的正实数:“^[0-9]+(.[0-9]{2})?$”
437     // 只能输入有1-3位小数的正实数:“^[0-9]+(.[0-9]{1,3})?$”
438     // 只能输入非零的正整数:“^+?[1-9][0-9]*$”
439     // 只能输入非零的负整数:“^-[1-9][0-9]*$”
440     // 只能输入长度为3的字符:“^.{3}$”
441     // 只能输入由26个英文字母组成的字符串:“^[A-Za-z]+$”
442     // 只能输入由26个大写英文字母组成的字符串:“^[A-Z]+$”
443     // 只能输入由26个小写英文字母组成的字符串:“^[a-z]+$”
444     // 只能输入由数字和26个英文字母组成的字符串:“^[A-Za-z0-9]+$”
445     // 只能输入由数字、26个英文字母或者下划线组成的字符串:“^w+$”
446     // 验证用户密码:“^[a-zA-Z]\\w{5,17}$”正确格式为:以字母开头,长度在6-18之间,
447     //
448     // 只能包含字符、数字和下划线。
449     // 验证是否含有^%&’,;=?$”等字符:“[^%&’,;=?$x22]+”
450     // 只能输入汉字:“^[u4e00-u9fa5],{0,}$”
451     // 验证Email地址:“^w+[-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$”
452     // 验证InternetURL:“^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$”
453     // 验证电话号码:“^((d{3,4})|d{3,4}-)?d{7,8}$”
454     //
455     // 正确格式为:“ XXXX-XXXXXXX”,“XXXX-XXXXXXXX”,“XXX-XXXXXXX”,
456     //
457     // “XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。
458     // 验证身份证号(15位或18位数字):“^d{15}|d{}18$”
459     // 验证一年的12个月:“^(0?[1-9]|1[0-2])$”正确格式为:“01”-“09”和“1”“12”
460     // 验证一个月的31天:“^((0?[1-9])|((1|2)[0-9])|30|31)$” 正确格式为:“01”“09”和“1”“31”。
461     //
462     // 匹配中文字符的正则表达式: [u4e00-u9fa5]
463     // 匹配双字节字符(包括汉字在内):[^x00-xff]
464     // 匹配空行的正则表达式:n[s| ]*r
465     // 匹配HTML标记的正则表达式:/< (.*)>.*|< (.*) />/
466     // 匹配首尾空格的正则表达式:(^s*)|(s*$)
467     // 匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
468     // 匹配网址URL的正则表达式:^http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$
469 }

猜你喜欢

转载自www.cnblogs.com/jasonandy/p/9184845.html