Java常用的一些正则表达式验证

View Code
 1 /**
 2      * 判断Ip地址是否合法
 3      * @param ip
 4      * @return
 5      */
 6     public static boolean isIp(String ip){
 7         if(ip == null){
 8             return false;
 9         }
10         String regex = "(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)){3}";
11         Pattern pattern = Pattern.compile(regex);
12         return pattern.matcher(ip).matches();
13     }
14     
15     /**
16      * 判断手机号是否合法
17      * @return
18      */
19     public static boolean isMobile(String mobile){
20         if(mobile == null){
21             return false;
22         }
23         String regex = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
24         Pattern pattern = Pattern.compile(regex);
25         return pattern.matcher(mobile).matches();
26     }
27     
28     /**
29      * 判断固话是否合法
30      * @param mobile
31      * @return
32      */
33     public static boolean isTele(String tele){
34         String regex = "^((0\\d{2,3})-)(\\d{7,8})(-(\\d{3,}))?$";
35         Pattern pattern = Pattern.compile(regex);
36         return pattern.matcher(tele).matches();
37     }
38     
39     /**
40      * 判断邮箱是否合法
41      * @param email
42      * @return
43      */
44     public static boolean isEmail(String email){
45         if(email==null){
46             return false;
47         }
48         String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
49         Pattern pattern = Pattern.compile(regex);
50         return pattern.matcher(email).matches();
51     }
52     /**
53      * 验证身份证是否合法
54      * @return
55      */
56     public static boolean isCard(String card){
57         if(card == null){
58             return false;
59         }
60         String regex = "^\\d{15}$|^\\d{17}(?:\\d|x|X)$";
61         Pattern pattern = Pattern.compile(regex);
62         return pattern.matcher(card).matches();
63     }
64     
65     /**
66      * 判断邮政编码是否合法
67      * @param mess
68      * @return
69      */
70     public static boolean isPastCode(String mess){
71         if(mess == null){
72             return false;
73         }
74         String regex = "[1-9]\\d{5}(?!\\d)";
75         Pattern pattern = Pattern.compile(regex);
76         return pattern.matcher(mess).matches();
77     }
原文地址:https://www.cnblogs.com/hujia/p/2975422.html