Java代码使用正则验证和常用工具方法

1.正则验证邮箱

  public static boolean checkEmail(String email){  
     boolean flag = false;  
     try{  
        String check = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$";
      Pattern regex = Pattern.compile(check);  
      Matcher matcher = regex.matcher(email);  
      flag = matcher.matches();  
     }catch(Exception e){  
      flag = false;  
     }  
     return flag;  
    }  

2.验证昵称是否合法

   /** 
     * 验证用户名,支持中英文(包括全角字符)、数字、下划线和减号 (全角及汉字算两位),长度为4-20位,中文按二位计数 
     * @return 
     */  
    public static boolean validateUserName(String userName) {  
    	
        String validateStr = "^[\w\--_[0-9]u4e00-u9fa5uFF21-uFF3AuFF41-uFF5A]+$";  
        boolean rs = false;  
        rs = matcher(validateStr, userName);  
        if (rs) {  
            int strLenth = getStrLength(userName);  
            if (strLenth < 4 || strLenth > 15) {  
                rs = false;  
            }  
        }  
        return rs;  
    }  
  
    /** 
     * 获取字符串的长度,对双字符(包括汉字)按两位计数 
     * @return 
     */  
    public static int getStrLength(String value) {  
        int valueLength = 0;  
        String chinese = "[u0391-uFFE5]";  
        for (int i = 0; i < value.length(); i++) {  
            String temp = value.substring(i, i + 1);  
            if (temp.matches(chinese)) {  
                valueLength += 2;  
            } else {  
                valueLength += 1;  
            }  
        }  
        return valueLength;  
    }  
  
    private static boolean matcher(String reg, String string) {  
        boolean tem = false;  
        Pattern pattern = Pattern.compile(reg);  
        Matcher matcher = pattern.matcher(string);  
        tem = matcher.matches();  
        return tem;  
    }  

3.判断是否是数字

   public static  boolean isInteger(String value) {  
	    	 Pattern p = Pattern.compile("^[0-9]{11}$");
	    	 Matcher m = p.matcher(value);
	    	 boolean isOK = m.find();
	    	 return isOK;
	 }

4.创建随机数

 public static int buildRandom(int length) {
	int num = 1;
	double random = Math.random();
	if (random < 0.1) {
		random = random + 0.1;
	}
	for (int i = 0; i < length; i++) {
		num = num * 10;
	}
	return (int) ((random * num));
	}

5.获取机型,浏览器类型

 	 public static  String getTypeas(HttpServletRequest request) {  
		 String typeas= request.getHeader("User-Agent"); 
			if (typeas.equals("")||typeas==null) {
				typeas="nothing";
			}
			return typeas;
	 }

5.获取IP地址

 	 public static  String getIp(HttpServletRequest request) {  
		 String ip = request.getHeader("x-forwarded-for");
	        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	            ip = request.getHeader("Proxy-Client-IP");
	        }
	        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	            ip = request.getHeader("WL-Proxy-Client-IP");
	        }
	        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	            ip = request.getRemoteAddr();
	        }
	        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
				ip="unknown";
			}
	        return ip;
	 }

6.判断数字是否在数组内

 	public static boolean isIn(Integer substring, Integer[] source) {
		if (source == null || source.length == 0) {
			return false;
		}
		for (int i = 0; i < source.length; i++) {
			Integer aSource = source[i];
			if (aSource.equals(substring)) {
				return true;
			}
		}
		return false;
	}
原文地址:https://www.cnblogs.com/jimmy-muyuan/p/5492689.html