Regular Expression that Matches values for YYYYMM
I need some regular expression for .net that matches the following pattern: YYYYMM
Where:
- YYYY is a year between 2000 and 2049
- MM is a month between 01 and 12
I have done the below one the problem with 开发者_C百科this, is that it includes invalid month values.
[2]{1}[0]{1}[0-4]{1}[0-9]{1}[0-1]{1}[0-9]{1}
any suggestion?
This should work: 20[0-4]\d(0[1-9]|1[0-2])
To match a month, you need to look for either:
- a
0
followed by anything from[1-9]
or - a
1
followed by anything from[0-2]
If you want want to capture the year and month, (20[0-4]\d)(0[1-9]|1[0-2])
If you don't want to capture either year or month, 20[0-4]\d(?:0[1-9]|1[0-2])
If you want to capture them with names, (?<year>20[0-4]\d)(?<month>0[1-9]|1[0-2])
(?<year>20[0-4]\d(?<month>(0[1-9])|(1[0-2])))
this is the regex I'll separate in two named groups
精彩评论