doushijia5684 2017-12-18 20:09
浏览 28
已采纳

正则表达式:检查十进制数[关闭]

I am have following regex to check decimals numbers and prevent long precision. I want to prevent precision from exceeding 18 digits. but actual number can be any long. Where is the error in this regex ?

^[+-]?([0-9]*[.])?(?([^.])([0-9]{1,18})|([0-9]))$

Demo: https://regex101.com/r/vC2yW4/38

  • 写回答

2条回答 默认 最新

  • douhushen3241 2017-12-18 20:14
    关注

    Brief

    The biggest issue you're having is that you're not actually matching anything with most of the pattern since you're using a positive lookahead (which is pretty much just an assertion ensuring that location matches whatever your lookahead contains), and then you're trying to assert the position as the end of the line $. Since none of your strings actually match the pattern of ^[+-]?([0-9]*[.])?$ you're not getting any results.

    Changing the positive lookahead to a simple capture group and removing $ you'll see now you have another issue. You're getting everything before . (which is good) but in the instance a . doesn't exist or in the case of the digits after a . you're only matching a single digit.

    Changing |\d to |\d{1,18} works for decimal places, but what about your whole numbers? Now a new issue has surfaced. Also note that your numbers with decimal places are properly being matched, but whole numbers are not (if they're above 18 digits).

    The reason, now, that you're not properly matching whole numbers is due to ([0-9]*[.])?. While the logic seems like it might work, it's actually ensuring a . exists before any number of digits. This prevents you from matching whole numbers that are longer than 18 digits. Now comes in my actual answer...


    Code

    See regex in use here

    ^[+-]?\d*\.?\d{1,18}$
    

    Results

    Input

    0781234567.1564529874654654231
    684984654657465
    0784543213
    .264556
    0.2564
    

    Output

    Only matches are shown below.

    684984654657465
    0784543213
    .264556
    0.2564
    

    Explanation

    • ^ Assert position at the start of the line
    • [+-]? Match zero or one of either + or -
    • \d* Match any number of digits
    • \.? Match zero or one of a literal dot character .
    • \d{1,18} Match between 1 and 18 of any digit
    • $ Assert position at the end of the line

    In Layman's terms we're matching any number of digits, then maybe a dot, then 1-18 numbers.

    1. But how does this work for whole numbers above 18 digits (let's say 19 digits)?
      • \d* actually matches 18 digits since it's possessive and then \d{1,18} matches the last digit.
    2. Ok, great, but how does it work for numbers below or equal to 18 digits (let's say 10 digits)?
      • \d{1,18} matches all 10 digits
    3. Ok and what about decimal numbers containing a whole number more than 18 digits long?
      • \d* matches any number of digits and then \. matches a literal dot, then \d{1,18} ensures a minimum of 1 decimal place and a maximum of 18 decimal places exists
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。