RegularUtil.java 2.16 KB
package com.cy.report.utils;

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

public class RegularUtil {
	
	/**
	 * 去掉空格,匹配只含有  "英文字母数字中文._- "的字符串.
	 * @param s
	 * @return
	 */
	public static boolean regularECNPL(String s) {
		s = s.replaceAll("\\s+","");
		String regex = "^[A-Za-z0-9\u4E00-\u9FA5._-]+$";
		boolean b = true;
		if (s.length() > 100) {
			b = false;
		}
		return s.matches(regex) && b;
	}
	/**
	 * 匹配密码  英文数字_,6到32位
	 * @param s
	 * @return
	 */
	public static boolean regularPass(String s) {
//		String regex1 = "^[a-zA-Z0-9._-]{5,15}$";
//		String regex2 = "[A-Z]";
//		String regex3 = "[a-z]";
//		String regex4 = "[1-9]";
//		String regex5 = "[-_.]";
//		Pattern pat1 = Pattern.compile(regex1);
//		Matcher mat1 = pat1.matcher(s);
//		
//		Pattern pat2 = Pattern.compile(regex2);
//		Matcher mat2 = pat2.matcher(s);
//		
//		Pattern pat3 = Pattern.compile(regex3);
//		Matcher mat3 = pat3.matcher(s);
//		
//		Pattern pat4 = Pattern.compile(regex4);
//		Matcher mat4 = pat4.matcher(s);
//		
//		Pattern pat5 = Pattern.compile(regex5);
//		Matcher mat5 = pat5.matcher(s);
//		
//		if (mat1.find() && mat2.find() && mat3.find() && mat4.find() && mat5.find()) {
//			return true;
//		} else {
//			return false;
//		}
//		return s.matches(".*?[^a-zA-Z\\d]+.*?")  
//        && s.matches(".*?[a-z]+.*?")  
//        && s.matches(".*?[A-Z]+.*?")  
//        && s.matches(".*?[\\d]+.*?")
//        && s.matches("^.{6,16}$");
		return s.matches("^[0-9a-zA-Z_]+$") && s.matches("^.{6,16}$");  
	}
	
	/**
	 * 验证email格式.
	 * @param s
	 * @return
	 */
	public static boolean regularEmail(String s) {
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		return s.matches(regex);
	}
	
	/**
	 * 验证url格式.
	 * @param s
	 * @return
	 */
	public static boolean regularURL(String s) {
		String regex = "(http|https|ftp)://([^/:]+)(:\\d*)?([^#\\s]*)";
		return s.matches(regex);
	}
	
	public static void main(String[] args) {
		String s = "aaaaaa";
		System.out.println(s.matches("^(?![0-9]+$)(?![a-z]+$)(([0-9a-z])|([0-9a-zA-Z])|([0-9a-z!@#$%^&*()])|([0-9a-zA-Z!@#$%^&*()])){6,16}$"));
	}
	 
}