正则判断代码片段

/**

 * 

 * 专门验证各种格式是否正确

 * 

 */

public class StringRegExUtil {

/**

* 判断字符是否为CJK字符集

* @param c

* @return

*/

public static boolean isChinese(char c) {

Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS

|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS

|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A

|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION

|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION

|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

return true;

}

return false;

}

/**

* 判断字符是否是一个中文

* @param c

* @return

*/

public static boolean isChinessSimple(char c) {

int v = (int) c;

return (v >= 19968 && v <= 171941);

}

/**

* 判断字符串是否全是数字

* @param str

* @return

*/

public static boolean isNumeric(String str) {

if (str.matches("\\d*")) {

return true;

} else {

return false;

}

}

/**

* 验证手机号码

* (验证长度不超过11位,大于6位,只能为数字)

* @param str

* @return

*/

public static boolean validPhoneNum(String str) {

try {

if(TextUtils.isEmpty(str)){

return false;

}

int length = str.trim().length();

if(length > 11 || length < 6 ){

return false;

}

Long.parseLong(str);

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

/**

* 验证邮政编码

* (长度6位,只能为数字)

* @param str

* @return

*/

public static boolean validPostCode(String str) {

try {

if(TextUtils.isEmpty(str)){

return false;

}

int length = str.trim().length();

if(length != 6 ){

return false;

}

Long.parseLong(str);

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

   /**

* 验证E_mail的合法性

* @param str

* @return

*/ 

public static boolean validMail(String str){

if(TextUtils.isEmpty(str)){

return false;

}

try {

String regex = "^([a-z0-9A-Z]+[-|._]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?.)+[a-zA-Z]{2,}$"; 

   Pattern   p   =   Pattern.compile(regex);     

   Matcher   m   =   p.matcher(str);     

   return m.find();  

} catch (Exception e) {

e.printStackTrace();

}

   

return false;

}

/**

* 验证身份证长度的合法性

* (只验证身份证长度15或18位)

* @param str

* @return

*/ 

public static boolean validIdCard(String str){

if(TextUtils.isEmpty(str)){

return false;

}

int length = str.trim().length();

if(length == 15 || length == 18){

return true;

}

return false;

}

/**

* 验证身份证号里面是否有除数字与字母x的身份证号

* @param str

* @return

*/

public static boolean validIdCardChar(String str) {

if(TextUtils.isEmpty(str)){

return false;

}

if (isNumeric(str)) {

return true;

} else {

String cTemp = str.substring(0, str.length() - 2);

if (cTemp != null && cTemp.length() == 17 && isNumeric(cTemp)) {

char endChar = str.charAt(str.length() - 1);

if (endChar == 'x' || endChar == 'X') {

return true;

}

}

}

return false;

}

/**

* 从字符串中获取中文的个数;

* @param str

* @return

*/

public static int countCharCNum (char[] cSum) {

if (cSum == null) return 0;

int i = 0;

for (char c : cSum) {

if (StringRegExUtil.isChinessSimple(c)) {

i++;

}

}

return i;

}

/**

* @param str从字符串中获取英文的个数;

* @return

*/

public static int countCharENum (char[] cSum) {

if (cSum == null) return 0;

int i = 0;

for (char c : cSum) {

if (Character.isLetter(c)) {

i++;

}

}

return i;

}

/**

* @param str从字符串中获取数字的个数;

* @return

*/

public static int countCharDNum (char[] cSum) {

if (cSum == null) return 0;

int i = 0;

for (char c : cSum) {

if (Character.isDigit(c)) {

i++;

}

}

return i;

}

}

猜你喜欢

转载自mayayeung.iteye.com/blog/2018269
今日推荐