是否是手机号码
public static boolean isMobilePhoneNumber(String txt) {
return matchRex(txt, "^((13[0-9])|(15[^4,//D])|(18[0,5-9]))//d{8}$");
}
是否是固定电话
public static boolean isTelephoneNumber(String txt) {
return matchRex(txt, "\\d{3}-\\d{8}|\\d{4}-\\d{7}");
}
是否是合法的IP地址
public static boolean isIPAddress(String txt) {
return matchRex(txt, "\\d+\\.\\d+\\.\\d+\\.\\d+");
}
是否是Email
public static boolean isEmail(String txt) {
return matchRex(txt, "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
}
是否是中国邮政编码
public static boolean isZNPostCode(String txt) {
return matchRex(txt, "[1-9]\\d{5}(?!\\d)");
}
是否是腾讯QQ号码
public static boolean isTencenQQNumber(String txt) {
return matchRex(txt, "[1-9][0-9]{4,}");
}
是否是URL
public static boolean isURL(String txt) {
return matchRex(txt, "[a-zA-z]+://[^\\s]*");
}
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线)
public static boolean canUserForAccount(String txt) {
return matchRex(txt, "^[a-zA-Z][a-zA-Z0-9_]{4,15}");
}
是否为居民身份证号码
public static boolean isZNIDCardNumber(String txt) {
return matchRex(txt, "\\d{15}|\\d{18}");
}
测试字符串是否符合某个正则表达式
public static boolean matchRex(String src, String rex) {
Pattern p = Pattern.compile(rex);
Matcher m = p.matcher(src);
return m.matches();
}
检测是否有中文字符
public static boolean isContainChinese(String txt) {
Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]");
Matcher m = p.matcher(txt);
if (m.find()) {
return true;
}
return false;
}