正实数的正则表达式

public static boolean isPositiveRealNum(String str){
	return str.matches("^\\d*(\\.\\d+)?$") 
			&& !str.matches("^0*(\\.0+)?$");
}

@Test
public void testPositive(){
	Assert.assertFalse(isPositiveRealNum("0.0"));
	Assert.assertFalse(isPositiveRealNum(".0000"));
	Assert.assertFalse(isPositiveRealNum("000000.0000"));
	Assert.assertFalse(isPositiveRealNum("0.0000000000000000"));
	Assert.assertTrue(isPositiveRealNum("1"));
	Assert.assertTrue(isPositiveRealNum("1.1"));
	Assert.assertTrue(isPositiveRealNum("0.1"));
	Assert.assertTrue(isPositiveRealNum("10000.2"));
	Assert.assertTrue(isPositiveRealNum("10000"));
	Assert.assertTrue(isPositiveRealNum("10000.904324"));
	Assert.assertFalse(isPositiveRealNum("-10000"));
}



尝试两条正则表达式写在一条中,暂时想不出。




你可能感兴趣的:(正则表达式,正整数)