- Regular Expressions Introduction
- Regular Expressions Exact Match
- Regular Expressions Examples
- Regular Expressions Range of Numbers
- Regular Expression Metacharacters
You want to match an integer number within a certain range of numbers. You want the regular expression to specify the range accurately, rather than just limiting the number of digits. The book Regular Expression Cookbook, Second Edition has some helpful examples, some of which are shown below.
Remember that between the square brackets we can have a range of digits. The pipe character (vertical line) means or.
1 to 12
^(1[0-2]|[1-9])$
1 to 24
^(2[0-4]|1[0-9]|[1-9])$
1 to 31
^(3[01]|[12][0-9]|[1-9])$
1 to 53
^(5[0-3]|[1-4][0-9]|[1-9])$
0 to 59
^[1-5]?[0-9]$
0 to 100
^(100|[1-9]?[0-9])$
1 to 100
^(100|[1-9][0-9]?)$
32 to 126
^(12[0-6]|1[01][0-9]|[4-9][0-9]|3[2-9])$
0 to 127
(12[0-7]|1[01][0-9]|[1-9]?[0-9])$