`

正则表达式验证

    博客分类:
  • java
 
阅读更多

 

package com.bos.irp.cash.action;
 
5. 
6.import java.util.regex.Matcher; 
7.import java.util.regex.Pattern; 
8. 
9./**
10. * @project baidamei
11. * @author cevencheng <cevencheng@gmail.com> www.zuidaima.com
12. * @create 2012-11-15 下午4:54:42
13. */ 
14.public class RegexUtils { 
15. 
16.     /**
17.     * 验证Email
18.     * @param email email地址,格式:zhangsan@zuidaima.com,zhangsan@xxx.com.cn,xxx代表邮件服务商
19.     * @return 验证成功返回true,验证失败返回false
20.     */  
21.    public static boolean checkEmail(String email) {  
22.        String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";  
23.        return Pattern.matches(regex, email);  
24.    }  
25.      
26.    /**
27.     * 验证身份证号码
28.     * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母
29.     * @return 验证成功返回true,验证失败返回false
30.     */  
31.    public static boolean checkIdCard(String idCard) {  
32.        String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";  
33.        return Pattern.matches(regex,idCard);  
34.    }  
35.      
36.    /**
37.     * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))
38.     * @param mobile 移动、联通、电信运营商的号码段
39.     *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)
40.     *、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)</p>
41.     *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p>
42.     *<p>电信的号段:133、153、180(未启用)、189</p>
43.     * @return 验证成功返回true,验证失败返回false
44.     */  
45.    public static boolean checkMobile(String mobile) {  
46.        String regex = "(\\+\\d+)?1[3458]\\d{9}$";  
47.        return Pattern.matches(regex,mobile);  
48.    }  
49.      
50.    /**
51.     * 验证固定电话号码
52.     * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447
53.     * <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字,
54.     *  数字之后是空格分隔的国家(地区)代码。</p>
55.     * <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号——
56.     * 对不使用地区或城市代码的国家(地区),则省略该组件。</p>
57.     * <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p>
58.     * @return 验证成功返回true,验证失败返回false
59.     */  
60.    public static boolean checkPhone(String phone) {  
61.        String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";  
62.        return Pattern.matches(regex, phone);  
63.    }  
64.      
65.    /**
66.     * 验证整数(正整数和负整数)
67.     * @param digit 一位或多位0-9之间的整数
68.     * @return 验证成功返回true,验证失败返回false
69.     */  
70.    public static boolean checkDigit(String digit) {  
71.        String regex = "\\-?[1-9]\\d+";  
72.        return Pattern.matches(regex,digit);  
73.    }  
74.      
75.    /**
76.     * 验证整数和浮点数(正负整数和正负浮点数)
77.     * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30
78.     * @return 验证成功返回true,验证失败返回false
79.     */  
80.    public static boolean checkDecimals(String decimals) {  
81.        String regex = "\\-?[1-9]\\d+(\\.\\d+)?";  
82.        return Pattern.matches(regex,decimals);  
83.    }   
84.      
85.    /**
86.     * 验证空白字符
87.     * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B
88.     * @return 验证成功返回true,验证失败返回false
89.     */  
90.    public static boolean checkBlankSpace(String blankSpace) {  
91.        String regex = "\\s+";  
92.        return Pattern.matches(regex,blankSpace);  
93.    }  
94.      
95.    /**
96.     * 验证中文
97.     * @param chinese 中文字符
98.     * @return 验证成功返回true,验证失败返回false
99.     */  
100.    public static boolean checkChinese(String chinese) {  
101.        String regex = "^[\u4E00-\u9FA5]+$";  
102.        return Pattern.matches(regex,chinese);  
103.    }  
104.      
105.    /**
106.     * 验证日期(年月日)
107.     * @param birthday 日期,格式:1992-09-03,或1992.09.03
108.     * @return 验证成功返回true,验证失败返回false
109.     */  
110.    public static boolean checkBirthday(String birthday) {  
111.        String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";  
112.        return Pattern.matches(regex,birthday);  
113.    }  
114.      
115.    /**
116.     * 验证URL地址
117.     * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80
118.     * @return 验证成功返回true,验证失败返回false
119.     */  
120.    public static boolean checkURL(String url) {  
121.        String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";  
122.        return Pattern.matches(regex, url);  
123.    }  
124.     
125.    /**
126.     * <pre>
127.     * 获取网址 URL 的一级域名
128.     * http://www.zuidaima.com/share/1550463379442688.htm ->> zuidaima.com
129.     * </pre>
130.     * 
131.     * @param url
132.     * @return
133.     */ 
134.    public static String getDomain(String url) { 
135.        Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE); 
136.        // 获取完整的域名 
137.        // Pattern p=Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE); 
138.        Matcher matcher = p.matcher(url); 
139.        matcher.find(); 
140.        return matcher.group(); 
141.    } 
142.    /**
143.     * 匹配中国邮政编码
144.     * @param postcode 邮政编码
145.     * @return 验证成功返回true,验证失败返回false
146.     */  
147.    public static boolean checkPostcode(String postcode) {  
148.        String regex = "[1-9]\\d{5}";  
149.        return Pattern.matches(regex, postcode);  
150.    }  
151.      
152.    /**
153.     * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小)
154.     * @param ipAddress IPv4标准地址
155.     * @return 验证成功返回true,验证失败返回false
156.     */  
157.    public static boolean checkIpAddress(String ipAddress) {  
158.        String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";  
159.        return Pattern.matches(regex, ipAddress);  
160.    }  
161.      
162.} 
 
  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics