首页 > 代码库 > java 正则表达式整形浮点型的判定
java 正则表达式整形浮点型的判定
//浮点型判断
public static boolean isDecimal(String str) {
if(str==null || "".equals(str))
return false;
java.util.regex.Pattern pattern = Pattern.compile("[0-9]*(\\.?)[0-9]*");
return pattern.matcher(str).matches();
}
//整型判断
public static boolean isInteger(String str){
if(str==null )
return false;
Pattern pattern = Pattern.compile("[0-9]+");
return pattern.matcher(str).matches();
}
浮点型测试用例:
public void testIsDecimal() {
assertTrue("123",Test.isDecimal("1"));
assertTrue("12.3",Test.isDecimal("12.3"));
assertTrue(".123",Test.isDecimal(".123"));
assertTrue("123.",Test.isDecimal("123."));
assertFalse("",Test.isDecimal(""));
assertFalse("null",Test.isDecimal(null));
assertFalse("abc", Test.isDecimal("abc"));
assertFalse("123abc", Test.isDecimal("123abc"));
assertFalse("abc123", Test.isDecimal("abc123"));
assertFalse("123.2.2", Test.isDecimal("123.2.2"));
}
java 正则表达式整形浮点型的判定