regexvwo

Regex, numbers below 20k


I'm looking for a regex to validate if numbers are below 20 000. I can't find the right solution, I have so far this:

(^([1-9]([0-9]{0,3})|20000)$)

Which works quite ok but as soon as it gets to 10 000 it gives no matches. So I have a gap from 9 999 - 20 000.

What am I doing wrong? I don't use regex for these situations, but the 3th party program required regex for such..

Thanks!


Solution

  • Your regex - ^([1-9]([0-9]{0,3})|20000)$ - matches numbers from 1 till 9999 and 20000.

    You may use

    ^([1-9]\d{0,3}|1\d{4}|20000)$
    

    See demo

    Breakdown: