频道栏目
首页 > 资讯 > Java > 正文

java正则表达式验证邮箱、IP地址、手机号码

12-11-01        来源:[db:作者]  
收藏   我要投稿

1.java验证IP地址:
[html]
Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?! 
 
 
\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5]) 
 
 
\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b"); 
Matcher matcher = pattern.matcher("127.400.600.2"); //以验证127.400.600.2为例 
System.out.println(matcher.matches()); 

Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!


\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])


\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
Matcher matcher = pattern.matcher("127.400.600.2"); //以验证127.400.600.2为例
System.out.println(matcher.matches());

2. java验证日期时间,解决润月:
[html]
Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]? 
 
 
((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\- 
 
 
\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2
 
 
(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0? 
 
 
[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))| 
 
 
(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0- 
 
 
9])((\\s)|(\\:([0-5]?[0-9])))))?$"); 
Matcher matcher = pattern.matcher("2000-02-29 23:59:59"); 
System.out.println(matcher.matches()); 

Pattern pattern = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?


((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-


\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}


(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?


[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|


(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-


9])((\\s)|(\\:([0-5]?[0-9])))))?$");
Matcher matcher = pattern.matcher("2000-02-29 23:59:59");
System.out.println(matcher.matches());

3.java验证邮箱格式:
[html] view plaincopyprint?Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0 
 
 
-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); 
Matcher matcher = pattern.matcher("a@aa.com"); 
System.out.println(matcher.matches()); 

Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0


-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
Matcher matcher = pattern.matcher("a@aa.com");
System.out.println(matcher.matches());

 
根据实际开发于2009年9月7日最新统计:
 中国电信发布中国3G号码段:中国联通185,186;中国移动188,187;中国电信189,180共6个号段。
 3G业务专属的180-189号段已基本分配给各运营商使用, 其中180、189分配给中国电信,187、188归中国


移动使用,185、186属于新联通。
 中国移动拥有号码段:139、138、137、136、135、134、159、158、157(3G)、152、151、150、188(


3G)、187(3G);14个号段
 中国联通拥有号码段:130、131、132、155、156(3G)、186(3G)、185(3G);6个号段
 中国电信拥有号码段:133、153、189(3G)、180(3G);4个号码段
 移动:
     2G号段(GSM网络)有139,138,137,136,135,134(0-8),159,158,152,151,150
     3G号段(TD-SCDMA网络)有157,188,187
     147是移动TD上网卡专用号段.
 联通:
     2G号段(GSM网络)有130,131,132,155,156
     3G号段(WCDMA网络)有186,185
 电信:
     2G号段(CDMA网络)有133,153
     3G号段(CDMA网络)有189,180


检验手机号码和邮箱的类
[html]
ackage com.ccit.networld.ccs.common.util; 
import java.io.IOException;   
import java.util.regex.Matcher;   
import java.util.regex.Pattern;   
import org.apache.log4j.Logger; 
  
public class ClassPathResource {  
 private static final Logger logger = Logger.getLogger(ClassPathResource.class); 
    public static boolean isMobileNO(String mobiles){      
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");      
        Matcher m = p.matcher(mobiles);      
        logger.info(m.matches()+"---");      
        return m.matches();      
    }  
    
    public static boolean isEmail(String email){      
     String str="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A- 
 
 
Za-z]{2,3}([\\.][A-Za-z]{2})?$"; 
        Pattern p = Pattern.compile(str);      
        Matcher m = p.matcher(email);      
        logger.info(m.matches()+"---");      
        return m.matches();      
    }  
    public static void main(String[] args) throws IOException {      
        System.out.println(ClassPathResource.isEmail("121212121212@12-12.co-m.cn"));      
    }   
}  

package com.ccit.networld.ccs.common.util;
import java.io.IOException; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
import org.apache.log4j.Logger;
 
public class ClassPathResource {
 private static final Logger logger = Logger.getLogger(ClassPathResource.class);
    public static boolean isMobileNO(String mobiles){    
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");    
        Matcher m = p.matcher(mobiles);    
        logger.info(m.matches()+"---");    
        return m.matches();    
    }
  
    public static boolean isEmail(String email){    
     String str="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-


Za-z]{2,3}([\\.][A-Za-z]{2})?$";
        Pattern p = Pattern.compile(str);    
        Matcher m = p.matcher(email);    
        logger.info(m.matches()+"---");    
        return m.matches();    
    }
    public static void main(String[] args) throws IOException {    
        System.out.println(ClassPathResource.isEmail("121212121212@12-12.co-m.cn"));    
    } 
}

 

相关TAG标签
上一篇:内网用户通过公网IP地址访问内部服务器
下一篇:两种修改网卡物理地址的秘技
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站