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条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型