程序员一代码搬运工
【Java学习心得和Java学习技术个人总结】

java 常用正则帮助类



import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 常用的正则表达式
 * 
 */
public class RegexUtils {
	
	/**
	 * IP
	 */
	public final static String IP="^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
	
	/**
	 * 邮箱
	 */
	public final static String EMAIL="\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
	
	/**
	 * 数字
	 */
	public final static String NUMBER="^[0-9]*$";
	
	/**
	 * 判断是否是正确的IP地址
	 * @param ip
	 * @return boolean true,通过,false,没通过
	 */
	public static boolean isIp(String ip) {
		if (null == ip || "".equals(ip))
			return false;
		return ip.matches(IP);
	}

	/**
	 * 判断是否是正确的邮箱地址
	 * 
	 * @param email
	 * @return boolean true,通过,false,没通过
	 */
	public static boolean isEmail(String email) {
		if (null == email || "".equals(email))
			return false;
		return email.matches(EMAIL);
	}


	/**
	 * 判断是否正整数
	 * 
	 * @param number
	 *            数字
	 * @return boolean true,通过,false,没通过
	 */
	public static boolean isNumber(String number) {
		if (null == number || "".equals(number))
			return false;
		return number.matches(NUMBER);
	}

	/**
	 * 判断几位小数(正数)
	 * 
	 * @param decimal
	 *            数字
	 * @param count
	 *            小数位数
	 * @return boolean true,通过,false,没通过
	 */
	public static boolean isDecimal(String decimal, int count) {
		if (null == decimal || "".equals(decimal))
			return false;
		String regex = "^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){" + count + "})?$";
		return decimal.matches(regex);
	}

	/**
	 * 判断是否是手机号码
	 * 
	 * @param phoneNumber
	 *            手机号码
	 * @return boolean true,通过,false,没通过
	 */
	public static boolean isPhoneNumber(String phoneNumber) {
		if (null == phoneNumber || "".equals(phoneNumber))
			return false;
		String regex = "^1[3|4|5|8][0-9]\\d{8}$";
		return phoneNumber.matches(regex);
	}

	/**
	 * 判断是否含有特殊字符
	 * 
	 * @param text
	 * @return boolean true,通过,false,没通过
	 */
	public static boolean hasSpecialChar(String text) {
		if (null == text || "".equals(text))
			return true;
		if (text.replaceAll("[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
			// 如果不包含特殊字符
			return false;
		}
		return true;
	}

	/**
	 * 适应CJK(中日韩)字符集,部分中日韩的字是一样的
	 */
	public static boolean isChinese2(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if (isChinese(c)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 内部方法,不可直接调用
	 * @param c
	 * @return
	 */
	private static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}
	
	  
    /**
     * 验证是否是用户名
     * 以字母开头,4-20位
     * @param userName
     * @return true 是 false 不是
     */
    public static boolean isUserName(String userName){      
        Pattern p = Pattern.compile("^[a-zA-Z]+([a-zA-Z0-9]|[_]){2,20}$"); 
        Matcher m = p.matcher(userName);
        return m.matches();
    } 
    
    /**
     * 验证是否是手机验证码
     * 6位数字
     * @param password
     * @return
     */
    public static boolean isPhoneProving(String phoneProving){       
        Pattern p = Pattern.compile("^[0-9]{6}$");  
        Matcher m = p.matcher(phoneProving); 
        return m.matches();      
    } 
	
    /**
     * 验证字符串是否是IMEI
     * @param str
     * @return
     */
    public static boolean isIMEI(String str){   
        Pattern p = Pattern.compile("^[0-9]{15}$");  
        Matcher m = p.matcher(str); 
        return m.matches();      
    } 
    
	public static void main(String[] args) {
		System.out.println(isDecimal("-0.2", 2));
	}

}

 

赞(0)
未经允许不得转载:程序员一代码搬运工 » java 常用正则帮助类
分享到: 更多 (0)