详解Java判断是否是整数,小数或实数的正则表达式
经常会遇到这样的情况,需要判断一个字符串是否是一个合法的数,包括整数,小数或者实数。
网上查到很多文章大多是判断这个字符串是否全为数字,比如下面这段来自stringutils的代码,可以看到,13.2这样的数字实际上会返回false,可是,他的确是一个数字。
public static boolean isnumeric(string str) { if (str == null) { return 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 com.sap.cesp.creditinsight.web.app.util; import java.util.regex.matcher; import java.util.regex.pattern; public class numbervalidationutils { private static boolean ismatch(string regex, string orginal){ if (orginal == null || orginal.trim().equals("")) { return false; } pattern pattern = pattern.compile(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 com.sap.cesp.creditinsight.web.app.util; import junit.framework.assert; import org.junit.test; public class numbervalidationutilstest { /** * test method for {@link com.sap.cesp.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 com.sap.cesp.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 com.sap.cesp.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 com.sap.cesp.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 com.sap.cesp.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 com.sap.cesp.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 com.sap.cesp.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")); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 大数乘法--蓝桥杯