locked
Regex for number in range

    Question

  • I need regex for validating if given string is number between 100 and 999?

    Daniel
    Saturday, September 15, 2007 3:46 PM

Answers

  • The or operator | as used in my second regexp allows you to or multiple options together.

     

    Saturday, September 15, 2007 4:46 PM

All replies

  • Hi, I think [1-9]0-9][0-9] will do this provided you mean 100 to 999 inclusive.  If you don't, if you really want numbers between 100 and 999 so 101 to 998 inclusive then something like 10[1-9]|1[1-8][0-9]|[1-9]|[2-8][0-9][0-9]|9[0-8][0-9]|99[0-8] might be what you want.

     

    There is probably a clevered way of writting the second version!

     

    NB: None of the above has been tested

     

    Saturday, September 15, 2007 3:53 PM
  • That was a lighting fast response Smile

    Can I ask for one more: How to validate if number is in between 1900-2006 ?

    Daniel
    Saturday, September 15, 2007 4:07 PM
  • Hello Daniel...

     

    I believe the pattern for that would be something like:

     

    ^([1-9][0-9]{2})$

     

     

    Saturday, September 15, 2007 4:07 PM
  • Hi Christopher

    Your solution works perfect for first problem.
    I've tried to modify it to ^([1-2][0,9][0-9][0-6])$ to validate 1900-2006
    but last digit is wrong so e.g. 1999 is not included.
    I guess I need to separate 1900-1999 and 2000-2006 checks ?
    Is it possible to make it inside same regex?

    Daniel
    Saturday, September 15, 2007 4:30 PM
  • The or operator | as used in my second regexp allows you to or multiple options together.

     

    Saturday, September 15, 2007 4:46 PM
  • Yes and here is final regex
    ^([1][9][0-9]{2})$|^([2][0]{2}[0-6])$

    Thanks
    Saturday, September 15, 2007 4:54 PM