^1001-9dd12$ 这个正则表达式怎么调整为不包含0和100并且支持小数并用java进行测试
可以将正则表达式修改为:^(0\.\d{1,2}|[1-9]\d?\.\d{1,2}|[1-9]\d?)%$
这个正则表达式匹配的是0.xx%、xx.xx%和xx%的格式,不包括0%和100%。
Java代码测试示例:
String regex = "^(0\\.\\d{1,2}|[1-9]\\d?\\.\\d{1,2}|[1-9]\\d?)%$";
Pattern pattern = Pattern.compile(regex);
String[] tests = {"0%", "50%", "99.99%", "100%", "0.1%", "1.23%", "50.5%"};
for (String test : tests) {
Matcher matcher = pattern.matcher(test);
System.out.println(test + " matches: " + matcher.matches());
}
输出结果:
0% matches: false
50% matches: true
99.99% matches: true
100% matches: false
0.1% matches: true
1.23% matches: true
50.5% matches: true
原文地址: https://www.cveoy.top/t/topic/7Ta 著作权归作者所有。请勿转载和采集!