日期:2014-05-20 浏览次数:20951 次
String str="-1.001";
boolean flag=str.matches("(-?)(\\d+|([0]|[1-9]\\d+)\\.\\d+)");
System.out.print(flag);
------解决方案--------------------
-?([0]|([1-9]\\d+))(\\.\\d+)?
整数部分要不就一个0,要不就是以1-9开头的数字,小数点及分数部分可有可无。
------解决方案--------------------
String str="-101.1";
System.out.print(str.matches("[-+]?([1-9]\\p{Digit}*|[0])(.\\p{Digit}*[1-9])?"));
------解决方案--------------------
String str="-1";
boolean flag=str.matches("(-?)([0]|[1-9]\\d*)(\\.\\d+)?");
System.out.print(flag);
------解决方案--------------------
public class Test {
static void test(String input){
System.out.println(input.matches("(-?)([0]|([1-9]\\d+))([.]\\d+)?"));
}
public static void main(String[] args) {
test("123");
test("-123");
test("11.22");
test("-11.22");
test("0.123");
test("-0.123");
test("-11.");
test("11.22.33");
test("-01.2");
test(".123");
test("a.2");
}
}