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

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

Regular Expression Regex for Format MMAYY - A is any alphabet and MM as month and YY as year

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!

question from:https://stackoverflow.com/questions/65661992/regular-expression-regex-for-format-mmayy-a-is-any-alphabet-and-mm-as-month-an

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

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...