John Smith
John Smith

Reputation: 8851

Regular expression to match 1 through 12 with padding?

I'm looking for a regular expression to match any of the following: 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12

Normally I would just do [1-12] but I need the padding of the zero if it is only one digit. Having a bit of a brainfart on how to do that elegantly.

Upvotes: 0

Views: 156

Answers (2)

i alarmed alien
i alarmed alien

Reputation: 9530

With some nice capturing matching fun:

/(0[1-9]|1[0-2])/

Upvotes: 1

hwnd
hwnd

Reputation: 70732

You can use the following regex.

^0[1-9]|1[012]$

Upvotes: 2

Related Questions