Java常用正则表达式验证工具类RegexUtils.java
  1 import java.util.regex.Matcher;
  2 import java.util.regex.Pattern;
  3 
  4 public class RegexUtils{
  5 
  6     /**
  7      * 正则表达式验证字符串
  8      * 
  9      * @param reg
 10      * @param str
 11      * @return
 12      */
 13     private final static boolean match(String regex, String str) {
 14         if (null == str || "".equals(str)) {
 15             return false;
 16         } else {
 17             Pattern p = Pattern.compile(regex);
 18             Matcher m = p.matcher(str);
 19             return m.matches();
 20         }
 21     }
 22 
 23     /**
 24      * 验证Email
 25      * 
 26      * @param email
 27      *            email地址,格式:i@510blog.cn.com,zhangsan@xxx.com.cn, xxx代表邮件服务商
 28      * @return 验证成功返回true,验证失败返回false
 29      */
 30     public static  boolean isEmail(String email) {
 31         String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?"; // 复杂匹配
 32         return match(regex, email);
 33     }
 34 
 35     /**
 36      * 验证URL地址
 37      * 
 38      * @param url
 39      *            格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或
 40      *            http://www.csdn.net:80
 41      * @return 验证成功返回true,验证失败返回false
 42      */
 43     public static boolean isUrl(String url) {
 44         String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";
 45         return match(regex, url);
 46     }
 47 
 48     /**
 49      * 验证固定电话号码
 50      * 
 51      * @param phone
 52      *            电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447
 53      *            <p>
 54      *            <b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9
 55      *            的一位或多位数字, 数字之后是空格分隔的国家(地区)代码。
 56      *            </p>
 57      *            <p>
 58      *            <b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号——
 59      *            对不使用地区或城市代码的国家(地区),则省略该组件。
 60      *            </p>
 61      *            <p>
 62      *            <b>电话号码:</b>这包含从 0 到 9 的一个或多个数字
 63      *            </p>
 64      * @return 验证成功返回true,验证失败返回false
 65      */
 66     public static boolean isPhone(String phone) {
 67         String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
 68         return match(regex, phone);
 69     }
 70 
 71     /**
 72      * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))
 73      * 
 74      * @param mobile
 75      *            移动、联通、电信运营商的号码段
 76      *            <p>
 77      *            移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)
 78      *            、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)
 79      *            </p>
 80      *            <p>
 81      *            联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)
 82      *            </p>
 83      *            <p>
 84      *            电信的号段:133、153、180(未启用)、189
 85      *            </p>
 86      * @return 验证成功返回true,验证失败返回false
 87      */
 88     public static boolean isMobile(String mobile) {
 89         String regex = "(\\+\\d+)?1[3458]\\d{9}$";
 90         return match(regex, mobile);
 91     }
 92 
 93     /**
 94      * 判断是否为数值
 95      * 
 96      * @param s
 97      * @return
 98      */
 99     public static boolean isNumeric(String numeric) {
100         String regex = "^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$";
101         return match(regex, numeric);
102     }
103 
104     /**
105      * 验证整数(正整数和负整数)
106      * 
107      * @param digit
108      *            一位或多位0-9之间的整数
109      * @return 验证成功返回true,验证失败返回false
110      */
111     public static boolean checkDigit(String digit) {
112         String regex = "\\-?[1-9]\\d+";
113         return match(regex, digit);
114     }
115 
116     /**
117      * 判断是否为数字字符串
118      * 
119      * @param digit
120      * @return 验证成功返回true,验证失败返回false
121      */
122     public static boolean isDigitString(String digit) {
123         String regex = "^[0-9]*$";
124         return match(regex, digit);
125     }
126 
127     /**
128      * 验证IP
129      * 
130      * @param ip
131      * @return
132      */
133     public static boolean isIP(String ip) {
134         String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
135         String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
136         return match(regex, ip);
137     }
138 
139     /**
140      * 匹配中国邮政编码
141      * 
142      * @param postcode
143      *            邮政编码
144      * @return 验证成功返回true,验证失败返回false
145      */
146     public static boolean isPostcode(String postcode) {
147         String regex = "[1-9]\\d{5}";
148         return match(regex, postcode);
149     }
150 
151     /**
152      * 验证车牌号
153      * 
154      * @param chePaiHao
155      * @return
156      */
157     public static boolean isChePaiHao(String chePaiHao) {
158         String regex1 = "^[\u4E00-\u9FA5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{5}$";
159         String regex2 = "^使[a-zA-Z_0-9]{6}$";
160         String regex3 = "^(BL)[a-zA-Z_0-9]{5}$";
161         return match(regex1, chePaiHao) || match(regex2, chePaiHao) || match(regex3, chePaiHao);
162     }
163 
164     /**
165      * 验证用户名,用户名只能由6到15位数字、字母、下划线组成!
166      * 
167      * @param username
168      * @return
169      */
170     public static boolean username(String username) {
171         String regex = "^([a-z]|[A-Z])[\\w]{6,20}$";
172         return match(regex, username);
173     }
174 
175     /**
176      * 验证中文姓名,两到四个汉字!
177      * 
178      * @param trueName
179      * @return
180      */
181     public static boolean trueName(String trueName) {
182         String regex = "^[\u4e00-\u9fa5]{2,4}$";
183         return match(regex, trueName);
184     }
185 
186     /**
187      * 验证密码,表示以匹配字母开头(不区分大小写),随后字符以字母、数字、下划线组成,长度 6 到 20 位
188      * 
189      * @param password
190      * @return
191      */
192     public static boolean password(String password) {
193         String regex = "^([a-z]|[A-Z])[\\w]{6,20}$";
194         return match(regex, password);
195     }
196 
197     /**
198      * 验证会员卡号,5129008866XXXXX
199      * 
200      * @param kh
201      * @return
202      */
203     public static boolean isHuiYuanKaHao(String kh) {
204         String regex = "^(5129008866)[0-9]{6}$";
205         return match(regex, kh);
206     }
207 
208     /**
209      * 判断str 是否是一个合法的Long型
210      * 
211      * @param str
212      * @return
213      */
214     public static boolean isLong(String str) {
215         try {
216             Long.parseLong(str);
217             return true;
218         } catch (NumberFormatException e) {
219             return false;
220         }
221     }
222 
223 }

 

posted on 2015-05-20 15:33  510blog  阅读(564)  评论(0)    收藏  举报