我正在研究一个regx问题。我已经有了像[0-9]*([.][0-9]{2})
这样的regx。这是一种非格式化验证。现在有了这个验证,我想要包括不应该提供0金额。像10是有效但0应该是无效的。
一些有效值的示例
10.00
10
1
无效
0
如果您提供一些答案或让我知道该怎么做,那么会被挪用。
答案 0 :(得分:3)
您可以按如下方式更改正则表达式:
[1-9][0-9]*([.][0-9]{2})*
这将需要在任何其他数字之前至少包含1到9之间的一个数字,以及可选的尾随十进制值。
示例强>
Pattern p = Pattern.compile("[1-9][0-9]*([.][0-9]{2})*");
String[] test = {"10.00", "10", "1", "0", "00.0"};
for (String s: test) {
System.out.printf("%s is matched? %b%n", s, p.matcher(s).find());
}
<强>输出强>
10.00 is matched? true
10 is matched? true
1 is matched? true
0 is matched? false
00.0 is matched? false