Skip to content
Advertisement

Regular expression to accept decimal numbers between 0 to 100

My requirement is a regular expression it accepts decimal values between 0 to 100 (like 1,2,3,….,99, 0.1,0.2,0.3,…..,99.9, 0.01,0.02,0.03,…..,99.99, 00.01 to 99.99). I found one solution

/^(?!0?0.00$)d{1,2}.d{2}$/ 

but it accepts only decimal values like 00.01 to 99.99.

Advertisement

Answer

How about:

^(?!0+(?:.0+)?$)d?d(?:.dd?)?$

Explanation:

^           : begining of string
  (?!       : negative lookahead, assumes there is no:
    0+      : 1 or more zero
    (?:     : non capture group
      .0+  : a dot then 1 or more zeros
    )?$     : end of group, optional, until end of string
  )         : end of lookahead
  d?d     : 1 or 2 digit
  (?:       : non capture group
    .dd? : a dot followed by 1 or 2 digit
  )?        : end of group, optional
$           : end of string
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement