输入字符为非0数字正则表达式怎么写 ?

解决方案 »

  1.   

    for example
    String s = "123";
    System.out.println(s.matches("[1-9]+"));
    [1-9]表示数字1-9,
    +表示出现1次或多次
      

  2.   

              //这个方法是验证非0数字的,如果是非0数字则输出ture、否则输出false。
               public static void chekNum(){
                    System.out.println("请输入一个数字:");
    String string = new Scanner(System.in).next();
    Pattern pattern = Pattern.compile("\\d+");
    Matcher matcher = pattern.matcher(string);
    if(matcher.matches() && Integer.parseInt(string) != 0)
    System.out.println("true");
    else
    System.out.println("false");
                }其中正则表达式\d表示为数字+号标识一个或多个,这些东西在Java API中都可以找到。
    希望对你有所帮助。加油!