中用正则表达式判断字符串是否全是数字"/>
Java中用正则表达式判断字符串是否全是数字
public boolean isNumeric(String str) {//Pattern pattern = Patternpile("^-?[0-9]+"); //这个也行Pattern pattern = Patternpile("^-?\\d+(\\.\\d+)?$");//这个也行Matcher isNum = pattern.matcher(str);if (!isNum.matches()) {return false;}return true;}
以上两种正则表达式均正确
System.out.println(new Test().isNumeric("sd21"));//falseSystem.out.println(new Test().isNumeric("2113 23"));//falseSystem.out.println(new Test().isNumeric(""));//falseSystem.out.println(new Test().isNumeric("12#12"));//falseSystem.out.println(new Test().isNumeric("-11212"));//trueSystem.out.println(new Test().isNumeric("11212"));//trueSystem.out.println(new Test().isNumeric("11212SDS"));//false
更多推荐
Java中用正则表达式判断字符串是否全是数字
发布评论