java正则表达式常见验证

package com.pathfinder.util;

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

import org.apache.commons.lang3.StringUtils;

public class ValidateUtil {
	
	/**
	 * Email地址验证
	 */
	public static final String EMAIL_REGEX = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
	
	/**
	 * 中文字符验证
	 */
	public static final String CHINESE_CHARACTERS_REGEX = "[\\u4e00-\\u9fa5]";
	
	/**
	 * 数字验证
	 */
	public static final String NUMBER_REGEX = "[1-9]\\d*";
	
	/**
	 * 正浮点数验证
	 */
	public static final String FLOAT_NUMBER_REGEX = "[0-9]\\d*.\\d*|0.\\d*[1-9]\\d*";
	
	/**
	 * 手机号验证
	 */
	public static final String MOBILE_REGEX = "^1[3|4|5|7|8]\\d{9}$";
	
	/**
	 * 电话验证规则
	 */
	public static final String PHONE_REGEX = "^0\\d{2,3}-\\d{7,8}$";
	
	/**
	 * 座机电话验证
	 */
	public static final String TELEPHONE_REGEX = "^(0[0-9]{2,3}-)?(\\d{7,8}){1}(-\\d{2,5})?";
	
	/**
	 * 无区号电话验证规则
	 */
	public static final String NOACTEL_REGEX = "^\\d{7,8}$";
	
	/**
	 * QQ验证
	 */
	public static final String QQ_REGEX = "[1-9]([0-9]{5,11})";
	
	/**
	 *  邮箱是否符合正则表达式
	 * @param isOk
	 * @return boolean
	 */
	public static boolean isEmail(String email)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(email))
		{
			isOk = isMatche(email, ValidateUtil.EMAIL_REGEX);
		}
		return isOk;
	}
	
	/**
	 *  是否是中文
	 * @param chineseCharacter
	 * @return boolean
	 */
	public static boolean isChineseCharacter(String chineseCharacter)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(chineseCharacter))
		{
			String character ;
			for (int ni=0; ni < chineseCharacter.toCharArray().length; ni++)
			{
				character = String.valueOf(chineseCharacter.toCharArray()[ni]);
				if (!isMatche(character, ValidateUtil.CHINESE_CHARACTERS_REGEX))
				{
					isOk = false;
					break;
				}
				else
				{
					isOk = true;
				}
			}
		}
		return isOk;
	}
	
	/**
	 * 数字验证
	 * @param number
	 * @return boolean
	 */
	public static boolean isNumber(String number)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(number))
		{
			isOk = isMatche(number, ValidateUtil.NUMBER_REGEX);
		}
		return isOk;
	}
	
	/**
	 * 正浮点数验证
	 * @param floatNumber
	 * @return boolean
	 */
	public static boolean isFloatNumber(String floatNumber)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(floatNumber))
		{
			isOk = isMatche(floatNumber, ValidateUtil.FLOAT_NUMBER_REGEX);
		}
		return isOk;
	}
	
	/**
	 * 手机号验证
	 * @param mobile
	 * @return boolean
	 */
	public static boolean isMobile(String mobile)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(mobile))
		{
			isOk = isMatche(mobile, ValidateUtil.MOBILE_REGEX);
		}
		return isOk;
	}
	
	/**
	 * 电话验证规则
	 * @param phone
	 * @return boolean
	 */
	public static boolean isPhone(String phone)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(phone))
		{
			isOk = isMatche(phone, ValidateUtil.PHONE_REGEX);
		}
		return isOk;
	}
	
	/**
	 * 座机电话验证
	 * @param telephone
	 * @return boolean
	 */
	public static boolean isTelephone(String telephone)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(telephone))
		{
			isOk = isMatche(telephone, ValidateUtil.TELEPHONE_REGEX);
		}
		return isOk;
	}
	
	/**
	 * 无区号电话验证规则
	 * @param noactel
	 * @return boolean
	 */
	public static boolean isNoactel(String noactel)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(noactel))
		{
			isOk = isMatche(noactel, ValidateUtil.NOACTEL_REGEX);
		}
		return isOk;
	}
	
	/**
	 * QQ验证
	 * @param qq
	 * @return boolean
	 */
	public static boolean isQQ(String qq)
	{
		boolean isOk = false;
		if (!StringUtils.isBlank(qq))
		{
			isOk = isMatche(qq, ValidateUtil.QQ_REGEX);
		}
		return isOk;
	}
	
	/**
	 * 正则表达式是否匹配,匹配返回true,否则返回true
	 * @param s 字符串
	 * @param regex 正则表达式
	 * @return boolean
	 */
	public static boolean isMatche(String s, String regex){
		boolean isMatche = false;
		if (!StringUtils.isBlank(regex))
		{
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(s);
			isMatche = m.matches();
		}
		return isMatche;
	}
	
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
	
		
	}
}

你可能感兴趣的:(正则表达式验证)