天天看點

一個通用的Java正則比對工具(檢查郵箱名、電話号碼、使用者密碼、郵政編碼等合法性的工具類)

一個通用的Java正則比對工具(檢查郵箱名、電話号碼、使用者密碼、郵政編碼等合法性的工具類)。

一個通用且常用的Java正則比對工具,用以檢查郵箱名、電話号碼、使用者密碼、郵政編碼等合法性。

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

public	class RegexUtils {

	 /**
   * 驗證Email
   * @param email email位址,格式:[email protected][email protected],xxx代表郵件服務商
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkEmail(String email) { 
      String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?"; 
      return Pattern.matches(regex, email); 
  } 
   
  /**
   * 驗證身份證号碼
   * @param idCard 居民身份證号碼15位或18位,最後一位可能是數字或字母
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  
  public static boolean checkIdCard(String idCard) 
  { 
      String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}"; 
      return Pattern.matches(regex,idCard); 
  } 
   
  /**
   * 驗證手機号碼(支援國際格式,+86135xxxx...(中國内地),+00852137xxxx...(中國香港))
   * @param mobile 移動、聯通、電信營運商的号碼段
   *移動的号段:134(0-8)、135、136、137、138、139、147(預計用于TD上網卡)
   *、150、151、152、157(TD專用)、158、159、187(未啟用)、188(TD專用)
   *聯通的号段:130、131、132、155、156(世界風專用)、185(未啟用)、186(3g)
   *電信的号段:133、153、180(未啟用)、189
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkMobile(String mobile) { 
      String regex = "(\\+\\d+)?1[3458]\\d{9}$"; 
      return Pattern.matches(regex,mobile); 
  } 
   
  /**
   * 驗證固定電話号碼
   * @param phone 電話号碼,格式:國家(地區)電話代碼 + 區号(城市代碼) + 電話号碼,如:+8602085588447
   * 國家(地區) 代碼 :辨別電話号碼的國家(地區)的标準國家(地區)代碼。它包含從 0 到 9 的一位或多位數字,
   *  數字之後是空格分隔的國家(地區)代碼。
   * 區号(城市代碼):這可能包含一個或多個從 0 到 9 的數字,地區或城市代碼放在圓括号——
   * 對不使用地區或城市代碼的國家(地區),則省略該元件。
   * 電話号碼:這包含從 0 到 9 的一個或多個數字 
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkPhone(String phone) { 
      String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$"; 
      return Pattern.matches(regex, phone); 
  } 
   
  /**
   * 驗證整數(正整數和負整數)
   * @param digit 一位或多位0-9之間的整數
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkDigit(String digit) { 
      String regex = "\\-?[1-9]\\d+"; 
      return Pattern.matches(regex,digit); 
  } 
   
  /**
   * 驗證整數和浮點數(正負整數和正負浮點數)
   * @param decimals 一位或多位0-9之間的浮點數,如:1.23,233.30
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkDecimals(String decimals) { 
      String regex = "\\-?[1-9]\\d+(\\.\\d+)?"; 
      return Pattern.matches(regex,decimals); 
  }  
   
  /**
   * 驗證空白字元
   * @param blankSpace 空白字元,包括:空格、\t、\n、\r、\f、\x0B
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkBlankSpace(String blankSpace) { 
      String regex = "\\s+"; 
      return Pattern.matches(regex,blankSpace); 
  } 
   
  /**
   * 驗證中文
   * @param chinese 中文字元
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkChinese(String chinese) { 
      String regex = "^[\u4E00-\u9FA5]+$"; 
      return Pattern.matches(regex,chinese); 
  } 
   
  /**
   * 驗證日期(年月日)
   * @param birthday 日期,格式:1992-09-03,或1992.09.03
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkBirthday(String birthday) { 
      String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}"; 
      return Pattern.matches(regex,birthday); 
  } 
   
  /**
   * 驗證URL位址
   * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkURL(String url) { 
      String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?"; 
      return Pattern.matches(regex, url); 
  } 
  
  /**
   *      * 擷取網址 URL 的一級域名
   * http://detail.tmall.com/item.htm?spm=a230r.1.10.44.1xpDSH&id=15453106243&_u=f4ve1uq1092 ->> tmall.com
   * 
   * 
   * @param url
   * @return
   */
  public static String getDomain(String url) {
		Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
		// 擷取完整的域名
		// Pattern p=Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
		Matcher matcher = p.matcher(url);
		matcher.find();
		return matcher.group();
  }
  /**
   * 比對中國郵政編碼
   * @param postcode 郵政編碼
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkPostcode(String postcode) { 
      String regex = "[1-9]\\d{5}"; 
      return Pattern.matches(regex, postcode); 
  } 
   
  /**
   * 比對IP位址(簡單比對,格式,如:192.168.1.1,127.0.0.1,沒有比對IP段的大小)
   * @param ipAddress IPv4标準位址
   * @return 驗證成功傳回true,驗證失敗傳回false
   */ 
  public static boolean checkIpAddress(String ipAddress) { 
      String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))"; 
      return Pattern.matches(regex, ipAddress); 
  } 
   
  //是否包含 . 号
  public static boolean checkContainsDot(String username) { 
	   return	username.contains(".");
  } 
  
  //是否包含連詞符
  public static boolean checkContainsHyphen(String username) { 
	   return	username.contains("-");
  }
  
  //密碼長度 6-20
  public static boolean checkUserPasswordLength(String pwd) { 
	   return	pwd.length() > 5 && pwd.length() <21;
  }
  
  public static boolean isValidUserName(String un) 
  { 
	   String regex = "([A-Z0-9a-z-]|[\\u4e00-\\u9fa5])+";
	   return Pattern.matches(regex, un); 
  }
}