正则判断一个字符串是否为数字(包括整数和小数)

时间:2025-03-11 22:17:25

正则判断一个字符串是否为数字(包括整数和小数)\


public class IsNumber {
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("^(\\-|\\+)?\\d+(\\.\\d+)?$");//这个是对的
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
}

转载:/guanghuichenshao/article/details/80454185