Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need a regular expression to be used in the form. The rule is simple, the format must be MMAYY.

Example of valid MMAYY:

01A21
12B20
01A22

Example of invalid MMAYY:

01121
22A21

I tried the below but it will check for MMYY only.

^0[1-9]|^(11)|^(12)[0-9][0-9]$

Any help will be appreciated. Thank you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
299 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your regex is using alternation with incorrect grouping. Moreover you are not matching mont number 10 and you are also not matching a letter in the middle.

You may use this regex:

^(?:0[1-9]|1[0-2])[A-Z][0-9]{2}$

RegEx Demo

RegEx Demo

  • ^: Start
  • (?:0[1-9]|1[0-2]): Match a 2 digit number for month number, of the form 01, 02, 03, ... 10, 11, 12
  • [A-Z]: Match an uppercase letter
  • [0-9]{2}: Match 2 digits
  • $: End

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...