Java正则表达式验证手机号码、邮箱

一下内容来自:失宠的女巫 http://blog.sina.com.cn/yezhimeihuo
1.java验证IP地址:
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验证日期时间,解决润月:
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验证邮箱格式:
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("[email protected]");
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

检验手机号码和邮箱的类
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("[email protected]"));     
    }  
} 

猜你喜欢

转载自mok678-126-com.iteye.com/blog/1187533