java 正则校验

package com.luopan.util;

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

import org.apache.commons.lang.StringUtils;

/**
 * 验证器
 */
public class ValidatorUtil {
	public static boolean validateMobile(String mobile) {
		if (mobile != null && mobile.indexOf(",") >= 0) { // 群发的暂时不判断
			return true;
		}
		if (mobile == null || mobile.length() != 11) return false;
		Pattern p = Pattern.compile("^(13|14|15|17|18)\\d{9}");
		Matcher m = p.matcher(mobile);
		return m.matches();
	}

	public static boolean validateEmail(String email) {
		if (email == null) return false;
		Pattern p = Pattern.compile("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
		Matcher m = p.matcher(email);
		return m.matches();
	}

	public static boolean validateFax(String fax) {
		if (fax == null || fax.length() <= 7) {
			return false;
		}

		for (int i = 0; i < fax.length(); i++) {
			if ("1234567890-".indexOf(fax.charAt(i)) >= 0) {
				return false;
			}
		}

		// 北京传真号才能不加区号
		if (!fax.startsWith("0") && fax.length() != 8) {
			return false;
		}

		return true;
	}

	public static boolean validatePhone(String phone) {
		if (phone == null || phone.length() <= 7) {
			return false;
		}

		Pattern p = Pattern.compile("^(\\d)[\\-\\d]*\\d");
		Matcher m = p.matcher(phone);
		return m.matches();
	}

	public static boolean validateIp(String ip) {
		if (ip == null) return false;
		Pattern p = Pattern.compile("^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$");
		Matcher m = p.matcher(ip);
		return m.find();
	}
	public static boolean validateMac(String mac) {
		if (mac == null) return false;
		Pattern p = Pattern.compile("^[A-F0-9]{2}(-[A-F0-9]{2}){5}$");
		Matcher m = p.matcher(mac);
		return m.find();
	}
	/**
	 * 验证requestMac是否匹配bindMac,若匹配返回true,否则返回false
	 * @param bindMac 绑定的物理地址
	 * @param requestMac 待验证的物理地址
	 */
	public static boolean validBindMac(String bindMac, String requestMac) {
		// 无绑定MAC,则所有MAC都是正确的。
		if (StringUtils.isEmpty(bindMac)) {
			return true;
		}

		if (StringUtils.isEmpty(requestMac)) {
			return false;
		}
		// 是否绑定的标志,true为2个MAC匹配,false为不匹配。
		boolean validBindMac = true;

		String[] bindMacSplits = bindMac.split("-");
		String[] requestMacSplits = requestMac.split("-");
		int bindMacSplitsLength = bindMacSplits.length;
		int requestMacSplitsLength = requestMacSplits.length;
		int minLength = bindMacSplitsLength < requestMacSplitsLength ? bindMacSplitsLength : requestMacSplitsLength;

		for (int i = 0; i < minLength; i++) {
			// 标志为false,则跳出循环。
			if (!validBindMac) {
				break;
			}
			// "*"匹配所有
			if (bindMacSplits[i].indexOf("*") != -1) {
				continue;
			}
			// 对应的值是否相等。如果不等,则绑定标志置为false。
			if (!requestMacSplits[i].equalsIgnoreCase(bindMacSplits[i])) {
				validBindMac = false;
			}
		}

		return validBindMac;
	}

	public static boolean validBindMac(String[] bindMacs, String requestMac) {
		if (bindMacs == null || bindMacs.length == 0) {
			return true;
		}
		if (StringUtils.isEmpty(requestMac)) {
			return false;
		}
		boolean validBindMac = false;
		for (String bindMac : bindMacs) {
			// 如果有一个符合,则比较完成,退出循环,返回true。
			if (validBindMac) {
				break;
			}
			validBindMac = validBindMac(bindMac, requestMac);
		}
		return validBindMac;
	}

	public static boolean validBindMac(String bindMacsWithSeparator, String requestMac, String separator) {
		if (StringUtils.isEmpty(bindMacsWithSeparator)) {
			return true;
		}
		if (StringUtils.isEmpty(requestMac)) {
			return false;
		}
		if (StringUtils.isEmpty(separator)) {
			separator = ",";
		}
		String[] bindMacs = bindMacsWithSeparator.split(separator);
		return validBindMac(bindMacs, requestMac);
	}

	/**
	 * 验证requestIp是否匹配bindIp,匹配返回true,否则返回false
	 * @param bindIp 绑定的IP
	 * @param requestIp 待验证的IP,即登录IP
	 */
	public static boolean validBindIp(String bindIp, String requestIp) {
		// 无绑定IP,则所有IP都是正确的。
		if (StringUtils.isEmpty(bindIp)) {
			return true;
		}

		if (StringUtils.isEmpty(requestIp)) {
			return false;
		}
		// 是否绑定的标志,true为2个IP匹配,false为不匹配。
		boolean validBindIp = true;
		// split 函数需要的参数是正则表达式,【.】在正则表达式有特殊含义,所以要转义。
		String[] bindIpSplits = bindIp.split("\\.");
		String[] requestIpSplits = requestIp.split("\\.");
		int bindIpSplitsLength = bindIpSplits.length;
		int requestIpSplitsLength = requestIpSplits.length;
		int minLength = bindIpSplitsLength < requestIpSplitsLength ? bindIpSplitsLength : requestIpSplitsLength;

		for (int i = 0; i < minLength; i++) {
			// 标志为false,则跳出循环。
			if (!validBindIp) {
				break;
			}
			// "*"匹配所有
			if (bindIpSplits[i].indexOf("*") != -1) {
				continue;
			}
			// 对应的值是否相等。如果不等,则绑定标志置为false。
			if (!requestIpSplits[i].equals(bindIpSplits[i])) {
				validBindIp = false;
			}
		}

		return validBindIp;
	}

	public static boolean validBindIp(String[] bindIps, String requestIp) {
		if (bindIps == null || bindIps.length == 0) {
			return true;
		}
		if (StringUtils.isEmpty(requestIp)) {
			return false;
		}
		boolean validBindIp = false;
		for (String bindIp : bindIps) {
			// 如果有一个符合,则比较完成,退出循环,返回true。
			if (validBindIp) {
				break;
			}
			validBindIp = validBindIp(bindIp, requestIp);
		}
		return validBindIp;
	}

	public static boolean validBindIp(String bindIpsWithSeparator, String requestIp, String separator) {
		if (StringUtils.isEmpty(bindIpsWithSeparator)) {
			return true;
		}
		if (StringUtils.isEmpty(requestIp)) {
			return false;
		}
		if (StringUtils.isEmpty(separator)) {
			separator = ",";
		}
		String[] bindIps = bindIpsWithSeparator.split(separator);
		return validBindIp(bindIps, requestIp);
	}

}

你可能感兴趣的:(JavaEE)