/**
* 判断字符创是否为空
* @param str 字符串
* @return boolean
*/
public static boolean isEmpty(String str){
String regex="[^\\s]{1,}";
return str.matches(regex);
}
/**
* 判断字符创是否为数字
* @param str 字符串
* @return boolean
*/
public static boolean isNumber(String str){
String regex="[0-9]*";
return str.matches(regex);
}
/**
* 判断字符创是否为浮点数
* @param str 字符串
* @return boolean
*/
public static boolean isDouble(String str){
String regex="[\\d]+\\.[\\d]+";
return str.matches(regex);
}
/**
* 判断字符创是否为正整数
* @param str 字符串
* @return boolean
*/
public static boolean isInt(String str){
String regex="/^[1-9]+\\d*$";
return str.matches(regex);
}
/**
* 判断字符创是否为手机号
* @param str 字符串
* @return boolean
*/
public static boolean isPhone(String str){
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
Matcher m = p.matcher(str);
return m.matches();
}
/**
* 判断字符创是否为邮箱
* @param str 字符串
* @return boolean
*/
public static boolean isEmail(String str){
Pattern p = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String[] args) {
System.out.println(Regex.isDouble("1"));
}
本文提供了一系列使用正则表达式进行数据验证的方法,包括检查字符串是否为空、是否为数字、是否为浮点数、是否为正整数、是否为手机号及是否为邮箱地址的有效性验证。
272

被折叠的 条评论
为什么被折叠?



