Search code examples
regexregex-groupregex-greedy

To match any number greater than 15 using regexp


I tried to match any number greater than 15 using the following regexp:

0*[1-9][6-9][0-9]*

But I can match only 2 digit number eg. I can unmatch successfully 12 or 13 (less than 15) , whereas I am unable to match 105, 124 etc..

Anyone help me out how to resolve this.


Solution

  • Any number greater than 15 is

    • Any number with 3 or more digits with possible leading 0's

    • Any number with 2 digits where the first digit in in the character class [2-9]

    • Any number with 2 digits where the first digit is 1 and the second digit in in the character class [6-9]

    From these three rules we can build the regex, assumes that what we are matching only contains digits

    /^0*(?:[1-9][0-9]{2,}|[2-9][0-9]|1[6-9])$/
    

    If you can't use an extended regex then the following should work

    /^0*[1-9][0-9][0-9][0-9]*|0*[2-9][0-9]|0*1[6-9]$/