简体   繁体   English

正则表达式排除某些错误的数字模式

[英]Regex to exclude certain wrong numbers pattern

I have an array of string in java that include good types of numbers:我在 java 中有一个字符串数组,其中包含良好的数字类型:

 String [] valid={"5","-1","0","0.0","8.0","1.5","0.7","-0.2","0.09","-0.15"};

And i have this regex to match and check if those are valid or not:我有这个正则表达式来匹配并检查它们是否有效:

    static boolean checkValidNum(String n){
        return n.matches("^-?\\d+(\\.\\d+)?");
    }

This regex is ok to check what is good and valid num but i want to exclude the following as valid nums:这个正则表达式可以检查什么是好的和有效的数字,但我想排除以下作为有效数字:

    String [] invalidNum = {"001","-00.2","-0","-0.0"};

What should i add to my regex to make it return false on those nums?我应该在我的正则表达式中添加什么以使其在这些数字上返回 false?

Provide some alternatives using |使用|提供一些替代方案, eg ,例如

static boolean checkValidNum(String n) {
    return n.matches("^-?([1-9]\\d*(\\.\\d+)?|0\\.\\d*[1-9]\\d*)|0(\\.0)?");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM