1. 程式人生 > >java正則判斷是否是數字(浮點數和整數)

java正則判斷是否是數字(浮點數和整數)

public static boolean isNumber(String str) {
        //採用正則表示式的方式來判斷一個字串是否為數字,這種方式判斷面比較全
        //可以判斷正負、整數小數

        boolean isInt = Pattern.compile("^-?[1-9]\\d*$").matcher(str).find();
        boolean isDouble = Pattern.compile("^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$").matcher(str).find();

        return
isInt || isDouble; }