[SOLVED] A regular expression (regex) for allowing numbers starts from 0 and after point two digit allow

Issue

I have regular expressions that allow numbers from 0 to 999999999999999999.99
which is allow

  1. After a point it will only be two-digit (eg. 151531.99)
  2. Allow Integer like 1, 2, 3565, 48784, 87848541, 151515

I have found one regex which is [+]?([0-9]*\.[0-9]+|[0-9])
But it does not look above condition.

Solution

This part |[0-9] is an alternative that matches a single digit.

In this part [0-9]*\.[0-9]+ the digits and to are optional so it can match a leading dot as well.

You can match 1-18 digits and optional dot and 2 digits. Using anchors to prevent a partial match.

^\+?\d{1,18}(?:\.\d{2})?$

Regex demo

Answered By – The fourth bird

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *