Regex. Match any number from 1 to 365

We are using RegEx Validator to validate the input from the text field.

The current RegEx expression checks if the current number is in the range from 1 to 999.

We just inherited this code and we need to change the range from 999 to 365. In short, we need to write a regex to check if the input is between 1 - 365.

Of course, we can just use RangeValidator or a custom validator. But we don’t want to do that because we don’t want to introduce "significant" changes.

Thanks!

EDIT:

We also need to catch the following patterns: 001 for 1019 for 19

0


a source to share


4 answers


Using regexes is certainly not the best way to test entire ranges, but here you go:

 ^([1-9][0-9]?|[12][0-9][0-9]|3[0-5][0-9]|36[0-5])$

[1-9][0-9]?     matches    1 -  99
[12][0-9][0-9]  matches  100 - 299
3[0-5][0-9]     matches  300 - 359
36[0-5]         matches  360 - 365

      



EDIT: with leading zeros (also matches strings like 00000000321):

 ^0*([1-9][0-9]?|[12][0-9][0-9]|3[0-5][0-9]|36[0-5])$

      

+12


a source


Allow for gap, ehm leap year. :-)



+4


a source


you really don't want to use regex for this, you should just use / ^ \ d {1,3} $ / and check the number as 1..365

as for a simple regex:

SNIP

      

ferdinands good :)

+2


a source


A small addition to Ferdinand's regex for trapping leading zeros:

^([0]{0,2}[1-9]|[0]?[1-9][0-9]|[12][0-9][0-9]|3[0-5][0-9]|36[0-5])$

[0]{0,2}[1-9]  - catches 001, 01, 1...
[0]?[1-9][0-9] - catches 010, 10...

      

Please note that it does not catch 0234 or 0019

+1


a source







All Articles