Regular Expression Match to test for a valid year

后端 未结 14 1722
清歌不尽
清歌不尽 2020-11-29 02:27

Given a value I want to validate it to check if it is a valid year. My criteria is simple where the value should be an integer with 4 characters. I know this is

14条回答
  •  遥遥无期
    2020-11-29 03:03

    If you need to match YYYY or YYYYMMDD you can use:

    ^((?:(?:(?:(?:(?:[1-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:[2468][048]|[13579][26])00))(?:0?2(?:29)))|(?:(?:[1-9]\d{3})(?:(?:(?:0?[13578]|1[02])(?:31))|(?:(?:0?[13-9]|1[0-2])(?:29|30))|(?:(?:0?[1-9])|(?:1[0-2]))(?:0?[1-9]|1\d|2[0-8])))))|(?:19|20)\d{2})$
    

提交回复
热议问题