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

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

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

if i understood you correctly, you want numbers with only 18 digits max, this can be solved with the following expression:

^[0-9.]{0,18}$

hope this helps