Regular expression for accepting integers, decimal values and limiting by maximum value


I need a regular expression that accept only integers or decimals from 1 to 9999.99. I tried this


but it is also accepting 10000.


There are a few issues in the pattern:

  • This part [1-9][0-9]{0,4} can match 1-5 digits as the quantifier is 0-4 times so it can match 10000 as well
  • You don’t need 9999|9999.99 as those can already be matched by 1-9][0-9]{0,4}
  • This part .d matches any char except a newline followed by a d char. You have to escape both to match a dot literally and a single digit
  • With those changes, you can omit the outer capture group

The updated pattern looks like:

  • ^ Start of string
  • [1-9]\d{0,3} Match a single digit 1-9 and repeat 0 to 3 times a digit 0-9
  • (?:\.\d{1,2})? Optionally match a . and 1 or 2 digits
  • $ End of string

Regex demo

Answered By – The fourth bird

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.