RegEx for value Range 1 to 365

What is RegEx for a range of values ​​from 1- 365

+3
source share
7 answers

Not suitable for regular expression, but if you insist:

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

This does not allow leading zeros. If you want to allow them, let me know.

The above expression may be slightly reduced to

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

but I believe that the first solution will be more readable. YMMV.

+3
source

You will need to indicate the possible combinations 1-9, 10-99, 100-299, 300-359, 360-365:

^([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-5])$
+6
source

:

^(?:[1-9]\d?|[12]\d{2}|3[0-5]\d|36[0-5])$
  • ^ $ .
  • (? ) .
  • |
  • [1-9]\d?     1 to 99
  • [12]\d{2}   100 - 299
  • 3[0-5]\d     300 - 359
  • 36 [0-5]             360 - 365
+5

1 XYZ

^(?!0)(?!\d{4}$)(?![X+1-9]\d{2}$)(?!X[Y+1-9]\d$)(?!XY[Z+1-9]$)\d+$

:

  • X, Y Z 9, X + 1 .. 10. , , 10, .
  • , .
  • 0es.

:

^(?!0)(?!\d{4}$)(?![4-9]\d{2}$)(?!3[7-9]\d$)(?!36[6-9]$)\d+$

:

(?!0\d*) - does not start with 0
(?!\d{4}$) - does not have 4 digits, i.e. between 1000 and infinity
(?![4-9]\d{2}$) - it not between 400 and 999
(?!3[7-9]\d$) - it not between 370 and 399
(?!36[6-9]$) - it not between 366 and 369

.

+1
^3(6[0-5]|[0-5]\d)|[12]\d\d|[1-9]\d|[1-9]$

05 :

^3(6[0-5]|[0-5]\d)|[12]?\d?\d$

P.S.: . ToInt(), < =, > =

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

, PCRE-style. , , .

- :

^[1-9]|[1-9][0-9]|[1-2][0-9]{2}|3[0-5][0-9]|36[0-5]$

( ):

  • 1-9.
  • 10-99.
  • 100-299.
  • 300-359.
  • 360-365.

, , . .

^[0-9]{1,3}$, , , 1 365 :

def isValidDayOtherThanLeapYear (s):
    if not s.matches ("^[0-9]{1,3}$"):
        return false
    n = s.toInteger()
    if n < 1 or n > 365:
        return false
    return true

, , , .

0
source

Source: https://habr.com/ru/post/1779343/


All Articles