可以使用以下代码来实现: String pattern = "^[1-9]\d*(\.\d{1,2})?$"; if (str.matches(pattern)) { // 满足条件 } else { // 不满足条件 }