Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to allow only number between 1 to 12

Regex to allow only number between 1 to 12

I am trying (12)|[1-9]\d? but its not working, please help as i am new to regular expression

like image 597
tanuj shrivastava Avatar asked Sep 07 '15 10:09

tanuj shrivastava


2 Answers

Something like

^([1-9]|1[012])$
  • ^ Anchors the regex at start of the string
  • [1-9] Matches 1 to 9

  • | Alternation, matches the previous match or the following match.

  • 1[012] Matches 10, 11, or 12
  • $ Anchors the regex at the end of the string.

Regex Demo

like image 66
nu11p01n73R Avatar answered Sep 30 '22 18:09

nu11p01n73R


Here's some readymade regex expressions for a bunch of different numbers within a certain range:

Range Label Regex
1 to 12 hour / month 1[0-2]|[1-9]
1 to 24 hour 2[0-4]|1[0-9]|[1-9]
1 to 31 day of month 3[01]|[12][0-9]|[1-9]
1 to 53 week of year 5[0-3]|[1-4][0-9]|[1-9]
0 to 59 min / sec [1-5]?[0-9]
0 to 100 percentage 100|[1-9]?[0-9]
0 to 127 signed byte 12[0-7]|1[01][0-9]|[1-9]?[0-9]
32 to 126 ASCII codes 12[0-6]|1[01][0-9]|[4-9][0-9]|3[2-9]
like image 21
KyleMit Avatar answered Sep 30 '22 19:09

KyleMit