Regular Expression Match to test for a valid year

后端 未结 14 1764
清歌不尽
清歌不尽 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:01

    In theory the 4 digit option is right. But in practice it might be better to have 1900-2099 range.

    Additionally it need to be non-capturing group. Many comments and answers propose capturing grouping which is not proper IMHO. Because for matching it might work, but for extracting matches using regex it will extract 4 digit numbers and two digit (19 and 20) numbers also because of paranthesis.

    This will work for exact matching using non-capturing groups:

    (?:19|20)\d{2}

提交回复
热议问题