java 正则表达式验证输入字符串是否合法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shy415502155/article/details/88838464

1.判断字符串是否可以转换为正整型,(+、- 或0开始)
    public static boolean isNumber(String str){
        String reg = "^[1-9][0-9]{0,}$";
        return str.matches(reg);
    }

2.  判断字符串是否可以转换为整型或浮点

    public static boolean isNumber(String str){
        String reg = "^[0-9]+(.[0-9]+)?$"; 
        return str.matches(reg);
    }

3.  判断字符串是否可以转换为所有的整数

public static boolean isNumber(String str){
    String reg = "^\-{0,1}[0-9]{1,}$"; 
    return str.matches(reg);
 }
 

猜你喜欢

转载自blog.csdn.net/shy415502155/article/details/88838464