我正在尝试检查字符串是否符合要求。 现在我有这样的东西:
String aaa = "322.155";
boolean match = aaa.matches("\\d{3}\\.\\d{3}"); //matches
String aaa = "32.155";
boolean match = aaa.matches("\\d{3}\\.\\d{3}"); //don't match
我想要的是让所有这些数字
1
2
999
22.22
333.23
1.012
匹配模式。 我应该使用什么模式?
boolean match = aaa.matches("\\d{1-3}\\.\\d{1-3}"); //isn't correct
最佳答案
另一种可能性:
"\\d{1,3}(\\.\\d{1,3})?"
匹配一到三位数字,以及可选的小数点和另外一到三位尾随数字。
1 matches
2 matches
999 matches
22.22 matches
333.23 matches
1.012 matches
.123 does not match
1234 does not match
123.1234 does not match
1..2 does not match
关于java - 匹配 float 或整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33039230/