詳解Java判斷是否是整數,小數或實數的Regex_Regex

來源:互聯網
上載者:User

經常會遇到這樣的情況,需要判斷一個字串是否是一個合法的數,包括整數,小數或者實數。

網上查到很多文章大多是判斷這個字串是否全為數字,比如下面這段來自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;   } 

當然,網上還能查到很多其他方式,諸如用Regex判斷是否0-9,用字元ascii碼判斷是否是數字以及用Double.parseDouble()是否拋出異常來判斷是否為數字。

事實上,除了最後一種方式能達到我們的要求,其他的都很難真正做到類似的判斷。但是最後一種方式也很難區別出到底是正整數,負整數,正小數還是負小數,而且,捕獲異常的方式實在是有些難看。

基於此原因,我自己寫了一個工具類,專門用作數的檢測,目前能夠檢測正整數,負整數,整數,正小數,負小數,小數以及實數,採用的仍然是Regex的方式,當然,如果有遺漏或者錯誤,歡迎聯絡我以便更正,同時也歡迎修改或使用這些代碼以便符合你的應用情境。

可以簡單講下正則的思想以便修改,

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

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.