详解Java判断是否是整数,⼩数或实数的正则表达式
经常会遇到这样的情况,需要判断⼀个字符串是否是⼀个合法的数,包括整数,⼩数或者实数。
⽹上查到很多⽂章⼤多是判断这个字符串是否全为数字,⽐如下⾯这段来⾃StringUtils的代码,可以看到,13.2这样的数字实际上会返回false,可是,他的确是⼀个数字。
public static boolean isNumeric(String str) {
if (str == null) {
时间正则表达式javareturn false;
}
int sz = str.length();
for (int i = 0; i < sz; i++) {
if (Character.isDigit(str.charAt(i)) == false) {
return false;
}
}
return true;
}
当然,⽹上还能查到很多其他⽅式,诸如⽤正则表达式判断是否0-9,⽤字符ascii码判断是否是数字以及⽤
Double.parseDouble()是否抛出异常来判断是否为数字。
事实上,除了最后⼀种⽅式能达到我们的要求,其他的都很难真正做到类似的判断。但是最后⼀种⽅式也很难区别出到底是正整数,负整数,正⼩数还是负⼩数,⽽且,捕获异常的⽅式实在是有些难看。
基于此原因,我⾃⼰写了⼀个⼯具类,专门⽤作数的检测,⽬前能够检测正整数,负整数,整数,正⼩数,负⼩数,⼩数以及实数,采⽤的仍然是正则表达式的⽅式,当然,如果有遗漏或者错误,欢迎联系我以便更正,同时也欢迎修改或使⽤这些代码以便符合你的应⽤场景。
可以简单讲下正则的思想以便修改,
1. 对于正整数⽽⾔,可以带+号,第⼀个数字不能为0
2. 对于负整数⽽⾔,必须带负号,第⼀个数字也不能为0
3. 对于整数⽽⾔,实际是由0,正整数和负整数组成的,所以偷个懒⽤前两个⽅法⼀起判断
4. 对于正⼩数⽽⾔,可以考带+号,并考虑两种情况,第⼀个数字为0和第⼀个数字不为0,第⼀个数字为0时,则⼩数点后⾯应该不为0,第⼀个数字不为0时,⼩数点后可以为任意数字
5. 对于负⼩数⽽⾔,必须带负号,其余都同上
6. 对于⼩数,可以带正负号,并且带⼩数点就⾏了,但是⾄少保证⼩数点有⼀边不为空,所以这⾥还是分左边不为空和右边不为空的情况
7. 实数⽐较简单,,要么是整数,要么是⼩数
package sp.creditinsight.web.app.util;
import Matcher;
import Pattern;
public class NumberValidationUtils {
private static boolean isMatch(String regex, String orginal){
if (orginal == null || im().equals("")) {
return false;
}
Pattern pattern = Patternpile(regex);
Matcher isNum = pattern.matcher(orginal);
return isNum.matches();
}
public static boolean isPositiveInteger(String orginal) {
return isMatch("^\\+{0,1}[1-9]\\d*", orginal);
}
public static boolean isNegativeInteger(String orginal) {
return isMatch("^-[1-9]\\d*", orginal);
}
public static boolean isWholeNumber(String orginal) {
return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal);
}
public static boolean isPositiveDecimal(String orginal){
return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal);
}
public static boolean isNegativeDecimal(String orginal){
return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal);
}
public static boolean isDecimal(String orginal){
return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal);
}
public static boolean isRealNumber(String orginal){
return isWholeNumber(orginal) || isDecimal(orginal);
}
}
测试⽤例如下:
package sp.creditinsight.web.app.util;
import junit.framework.Assert;
import org.junit.Test;
public class NumberValidationUtilsTest {
/**
* Test method for {@link sp.creditinsight.web.app.util.NumberValidationUtils#isPositiveInteger(java.lang.String)}    */
//correct test case: 1, 87653521123567
//wrong test case: 0.1, 0, 0123, -1, -0.1, ab
@Test
public void testIsPositiveInteger() {
Assert.assertTrue(NumberValidationUtils.isPositiveInteger("1"));
Assert.assertTrue(NumberValidationUtils.isPositiveInteger("+12"));
Assert.assertTrue(NumberValidationUtils.isPositiveInteger("87653521123567"));
Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0.1"));
Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0"));
Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0123"));
Assert.assertFalse(NumberValidationUtils.isPositiveInteger("-1"));
Assert.assertFalse(NumberValidationUtils.isPositiveInteger("-0.1"));
Assert.assertFalse(NumberValidationUtils.isPositiveInteger("ab"));
}
/**
* Test method for {@link sp.creditinsight.web.app.util.NumberValidationUtils#isNegativeInteger(java.lang.String)}    */
//correct test case: -1, -87653521123567
//wrong test case: 0.1, 0, 0123, 1, -0.1, -ab
@Test
public void testIsNegativeInteger() {
Assert.assertTrue(NumberValidationUtils.isNegativeInteger("-1"));
Assert.assertTrue(NumberValidationUtils.isNegativeInteger("-87653521123567"));
Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0.1"));
Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0"));
Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0123"));
Assert.assertFalse(NumberValidationUtils.isNegativeInteger("1"));
Assert.assertFalse(NumberValidationUtils.isNegativeInteger("-0.1"));
Assert.assertFalse(NumberValidationUtils.isNegativeInteger("ab"));
}
/**
* Test method for {@link sp.creditinsight.web.app.util.NumberValidationUtils#isWholeNumber(java.lang.String)}.    */
//correct test case: -1, 0, 1, 8673434231, -282464334
//wrong test case: 0.1, 0123, -0.1, ab
@Test
public void testIsWholeNumber() {
Assert.assertTrue(NumberValidationUtils.isWholeNumber("-1"));
Assert.assertTrue(NumberValidationUtils.isWholeNumber("0"));
Assert.assertTrue(NumberValidationUtils.isWholeNumber("1"));
Assert.assertTrue(NumberValidationUtils.isWholeNumber("+12"));
Assert.assertTrue(NumberValidationUtils.isWholeNumber("8673434231"));
Assert.assertTrue(NumberValidationUtils.isWholeNumber("-282464334"));
Assert.assertFalse(NumberValidationUtils.isWholeNumber("0123"));
Assert.assertFalse(NumberValidationUtils.isWholeNumber("0.1"));
Assert.assertFalse(NumberValidationUtils.isWholeNumber("-0.1"));
Assert.assertFalse(NumberValidationUtils.isWholeNumber("ab"));
}
/
**
* Test method for {@link sp.creditinsight.web.app.util.NumberValidationUtils#isPositiveDecimal(java.lang.String)}    */
//correct test case: 0.1, 0.132213, 1.0
//wrong test case: 1, 0.0, 0123, -1, -0.1
@Test
public void testIsPositiveDecimal() {
Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("0.1"));
Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("0.132213"));
Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("30.00"));
Assert.assertTrue(NumberValidationUtils.isDecimal("0."));
Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("+12.0"));
Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("0123"));
Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("1"));
Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("0.0"));
Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("ab"));
Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("-1"));
Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("-0.1"));
}
/**
* Test method for {@link sp.creditinsight.web.app.util.NumberValidationUtils#isNegativeDecimal(java.lang.String)}    */
//correct test case: -0.132213, -1.0
//wrong test case: 1, 0, 0123, -1, 0.1
@Test
public void testIsNegativeDecimal() {
Assert.assertTrue(NumberValidationUtils.isNegativeDecimal("-0.132213"));
Assert.assertTrue(NumberValidationUtils.isNegativeDecimal("-1.0"));
Assert.assertTrue(NumberValidationUtils.isDecimal("-0."));
Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("1"));
Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0"));
Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0123"));
Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0.0"));
Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("ab"));
Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("-1"));
Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0.1"));
}
/**
* Test method for {@link sp.creditinsight.web.app.util.NumberValidationUtils#isDecimal(java.lang.String)}.
*/
//correct test case: 0.1, 0.00, -0.132213
//wrong test case: 1, 0, 0123, -1, 0., ba
@Test
public void testIsDecimal() {
Assert.assertTrue(NumberValidationUtils.isDecimal("0.1"));
Assert.assertTrue(NumberValidationUtils.isDecimal("0.00"));
Assert.assertTrue(NumberValidationUtils.isDecimal("+0.0"));
Assert.assertTrue(NumberValidationUtils.isDecimal("-0.132213"));
Assert.assertTrue(NumberValidationUtils.isDecimal("0."));
Assert.assertFalse(NumberValidationUtils.isDecimal("1"));
Assert.assertFalse(NumberValidationUtils.isDecimal("0123"));
Assert.assertFalse(NumberValidationUtils.isDecimal("0"));
Assert.assertFalse(NumberValidationUtils.isDecimal("ab"));
Assert.assertFalse(NumberValidationUtils.isDecimal("-1"));
}
/
**
* Test method for {@link sp.creditinsight.web.app.util.NumberValidationUtils#isRealNumber(java.lang.String)}.
*/
//correct test case: 0.032213, -0.234, 0.0, 1, -1, 0
//wrong test case: 00.13, ab, +0.14
@Test
public void testIsRealNumber() {
Assert.assertTrue(NumberValidationUtils.isRealNumber("0.032213"));
Assert.assertTrue(NumberValidationUtils.isRealNumber("-0.234"));
Assert.assertTrue(NumberValidationUtils.isRealNumber("0.0"));
Assert.assertTrue(NumberValidationUtils.isRealNumber("1"));
Assert.assertTrue(NumberValidationUtils.isRealNumber("+0.14"));
Assert.assertTrue(NumberValidationUtils.isRealNumber("-1"));
Assert.assertTrue(NumberValidationUtils.isRealNumber("0.0"));
Assert.assertFalse(NumberValidationUtils.isRealNumber("00.13"));
Assert.assertFalse(NumberValidationUtils.isRealNumber("ab"));
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。