常用的正则表达式验证

1、验证是否是数字

 public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

2、验证是否是数字或小数

 public static boolean isNumber(String str){
        if(isEmpty(str)){
            return false;
        }
        String reg = "\\d+(\\.\\d+)?";
        return str.matches(reg);
    }

你可能感兴趣的:(java)