String str = "0-2334343"; if (!str.matches("^\\d*\\-\\d*$")) { System.out.println("字符串:"+str +"不符合格式!"); }else{ System.out.println("字符串:"+str +"符合格式!"); }