Detailed Java to determine whether is an integer, decimal or real number of regular expressions _ regular expressions

Source: Internet
Author: User
Tags assert

It is often the case that you need to determine whether a string is a valid number, including integers, decimals, or real numbers.

Most of the articles on the Internet to determine whether the string is all numbers, such as the following from the StringUtils code, you can see that 13.2 of such numbers will actually return false, but he is indeed a number.

  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; 
  } 

Of course, many other ways can be found on the web, such as using regular expressions to determine whether 0-9, using character ASCII to determine whether a number and whether double.parsedouble () throw an exception to determine whether it is a number.

In fact, other than the last way to meet our requirements, the other is hard to really do similar judgments. But the last one is hard to tell whether it's a positive integer, a negative integer, a positive decimal or a negative decimal, and the way to catch an exception is a bit ugly.

For this reason, I wrote a tool class myself, specifically used as a number of detection, currently able to detect positive integers, negative integers, integers, positive decimal, negative decimal, decimal and real numbers, the use of the regular expression is still the way, of course, if there are omissions or errors, welcome to contact me to correct, Also welcome to modify or use the code to fit your application scenario.

You can simply talk about regular thinking in order to modify,

1. For positive integers, you can take the + number, the first number cannot be 0

2. For negative integers, you must take a minus sign and the first number cannot be 0

3. For integers, it's actually made up of 0, positive integers and negative integers, so steal a lazy judge with the first two methods

4. For positive decimal, you can take the + number, and consider two cases, the first number is 0 and the first number is not 0, the first number is 0 o'clock, then the decimal point should not be followed by 0, the first number is not 0 o'clock, after the decimal point can be any number

5. For negative decimals, the minus sign must be taken and the rest are the same

6. For decimals, you can take the positive sign, and with a decimal point on the line, but at least to ensure that the decimal point is not empty, so here or the left is not empty and the right is not empty

7. The real number is simpler, either an integer or a decimal

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) | | ispositiveinteg ER (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); 
 } 
 
}

Test cases are as follows:

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.Num Bervalidationutils#ispositiveinteger (java.lang.String)} *///correct Test case:1, 87653521123567//wrong Test C ase:0.1, 0, 0123,-1, -0.1, AB @Test public void Testispositiveinteger () {assert.asserttrue (Numbervalidationu 
    Tils.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 (ja va.lang.String)} *///correct Test Case:-1, -87653521123567//wrong test case:0.1, 0, 0123, 1, -0.1,-ab @T 
    est 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 F/{@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 testispositived 
    Ecimal () {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 (ja va.lang.String)} * *//correct test case: -0.132213, -1.0//wrong test case:1, 0, 0123,-1, 0.1 @Test Publ 
    IC 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")); The/** * Test method is 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 Testi 
    Sdecimal () {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")); 
 } 
 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.