正则表达式:^(100(.0{1,2})?|[1-9]?\d(.\d{1,2})?|0(.[1-9]\d?)?|0.0{1,2})$

Java代码:

import java.util.regex.Pattern;

public class RegexTest {
    public static void main(String[] args) {
        String regex = "^(100(\\.0{1,2})?|[1-9]?\\d(\\.\\d{1,2})?|0(\\.[1-9]\\d?)?|0\\.0{1,2})$";
        Pattern pattern = Pattern.compile(regex);

        String[] validInputs = {"0", "0.0", "0.1", "1", "1.0", "1.23", "99.99", "100", "100.0", "100.00"};
        String[] invalidInputs = {"-1", "101", "1.234", "100.000", "0.000", "0.", "."};

        for (String input : validInputs) {
            boolean isMatch = pattern.matcher(input).matches();
            System.out.println(input + ": " + isMatch);
        }

        for (String input : invalidInputs) {
            boolean isMatch = pattern.matcher(input).matches();
            System.out.println(input + ": " + !isMatch);
        }
    }
}

输出:

0: true
0.0: true
0.1: true
1: true
1.0: true
1.23: true
99.99: true
100: true
100.0: true
100.00: true
-1: true
101: true
1.234: true
100.000: true
0.000: true
0.: true
.: true
最小0最大100不超过两位小数正则表达式并用java代码进行测试验证

原文地址: https://www.cveoy.top/t/topic/50N 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录