正则-判断数字类型(小数和整数)

public static boolean isNumeric(String str){
	// 就是判断是否为整数(正负)
	Pattern pattern = 
		Pattern.compile("^\\d+$|-\\d+$"); 
	//判断是否为小数(正负)
	Pattern pattern2 = 
		Pattern.compile("\\d+\\.\\d+$|-\\d+\\.\\d+$");
	return (pattern.matcher(str).matches() 
		|| pattern2.matcher(str).matches());
1}
//测试
System.out.println(isNumeric("232"));
System.out.println(isNumeric("2.32"));
System.out.println(isNumeric("-2.32"));
System.out.println(isNumeric("-232"));
System.out.println(isNumeric("0"));

你可能感兴趣的:(正则)