0

0

判断是否是整数,小数或实数正则有哪些写法

php中世界最好的语言

php中世界最好的语言

发布时间:2018-03-30 14:45:58

|

4150人浏览过

|

来源于php中文网

原创

这次给大家带来判断是否是整数,小数或实数正则有哪些写法,使用判断是否是整数,小数或实数正则的注意事项有哪些,下面就是实战案例,一起来看一下。

经常会遇到这样的情况,需要判断一个字符串是否是一个合法的数,包括整数,小数或者实数。

网上查到很多文章大多是判断这个字符串是否全为数字,比如下面这段来自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")); 
     
  } 
 
}

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Cutout.Pro抠图
Cutout.Pro抠图

AI批量抠图去背景

下载

JS的正则如何校验非零的正整数

JS里最基础的正则表达式使用详解

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

54

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

40

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

50

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

12

2026.01.31

漫画防走失登陆入口大全
漫画防走失登陆入口大全

2026最新漫画防走失登录入口合集,汇总多个稳定可用网址,助你畅享高清无广告漫画阅读体验。阅读专题下面的文章了解更多详细内容。

13

2026.01.31

php多线程怎么实现
php多线程怎么实现

PHP本身不支持原生多线程,但可通过扩展如pthreads、Swoole或结合多进程、协程等方式实现并发处理。阅读专题下面的文章了解更多详细内容。

1

2026.01.31

php如何运行环境
php如何运行环境

本合集详细介绍PHP运行环境的搭建与配置方法,涵盖Windows、Linux及Mac系统下的安装步骤、常见问题及解决方案。阅读专题下面的文章了解更多详细内容。

0

2026.01.31

php环境变量如何设置
php环境变量如何设置

本合集详细讲解PHP环境变量的设置方法,涵盖Windows、Linux及常见服务器环境配置技巧,助你快速掌握环境变量的正确配置。阅读专题下面的文章了解更多详细内容。

0

2026.01.31

php图片如何上传
php图片如何上传

本合集涵盖PHP图片上传的核心方法、安全处理及常见问题解决方案,适合初学者与进阶开发者。阅读专题下面的文章了解更多详细内容。

2

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 5万人学习

PostgreSQL 教程
PostgreSQL 教程

共48课时 | 8.2万人学习

Git 教程
Git 教程

共21课时 | 3.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号