正常正数负数都没问题
非法数字就不行
例如
1.1.1
1.a
.124
a.1

解决方案 »

  1.   

    String str="-1.001";
    boolean flag=str.matches("(-?)(\\d+|([0]|[1-9]+)\\.\\d+)");
    System.out.print(flag);
      

  2.   

    上面那个有问题
    String str="-1.001";
    boolean flag=str.matches("(-?)(\\d+|([0]|[1-9]\\d+)\\.\\d+)");
    System.out.print(flag);
    应该这样
      

  3.   

    我也写一个。String str="-101.1";
    System.out.print(str.matches("[-+]?([1-9]\\p{Digit}*|[0])(.\\p{Digit}*[1-9])?"));可以验证通过带正负号的数字,小数部分可有可无。数字前头后头(小数最后)不能有0,验证通过的数字举例:0  10    1.5   0.1
    验证不通过的数字举例:0.0   -00.1  +1.00   12.   .12   +.12   -0.  
      

  4.   

    得有个边界
    import java.util.regex.*;public class Test {
    static String input="";
    static Pattern p=Pattern.compile("^(-?)([0]|([1-9]\\d+))([.]\\d+)?$");
    static void test(){
    Matcher m=p.matcher(input);
    System.out.println(m.find());
    }
    public static void main(String[] args) {
    input="-11.22";
    test();
    input="11.";
    test();
    input="11.22.33";
    test();
    input="01.2";
    test();
    input=".123";
    test();
    }}
      

  5.   

            
    String str="-1";
    boolean flag=str.matches("(-?)([0]|[1-9]\\d*)(\\.\\d+)?");
    System.out.print(flag);
      

  6.   

    String str="11.";
    boolean flag=str.matches("(-?)([0]|[1-9]\\d*)(\\.\\d+?)");
    System.out.print(flag);
    修正版
      

  7.   

    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");
    }
    }
    似乎没问题啊