Regular Expression Match to test for a valid year

RegexStringValidationString Matching

Regex Problem Overview


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 not the best solution as it will not allow years before 1000 and will allow years such as 5000. This criteria is adequate for my current scenario.

What I came up with is

\d{4}$

While this works it also allows negative values.

How do I ensure that only positive integers are allowed?

Regex Solutions


Solution 1 - Regex

Years from 1000 to 2999

^[12][0-9]{3}$

For 1900-2099

^(19|20)\d{2}$

Solution 2 - Regex

You need to add a start anchor ^ as:

^\d{4}$

Your regex \d{4}$ will match strings that end with 4 digits. So input like -1234 will be accepted.

By adding the start anchor you match only those strings that begin and end with 4 digits, which effectively means they must contain only 4 digits.

Solution 3 - Regex

The "accepted" answer to this question is both incorrect and myopic.

It is incorrect in that it will match strings like 0001, which is not a valid year.

It is myopic in that it will not match any values above 9999. Have we already forgotten the lessons of Y2K? Instead, use the regular expression:

^[1-9]\d{3,}$

If you need to match years in the past, in addition to years in the future, you could use this regular expression to match any positive integer:

^[1-9]\d*$

Even if you don't expect dates from the past, you may want to use this regular expression anyway, just in case someone invents a time machine and wants to take your software back with them.

Note: This regular expression will match all years, including those before the year 1, since they are typically represented with a BC designation instead of a negative integer. Of course, this convention could change over the next few millennia, so your best option is to match any integer—positive or negative—with the following regular expression:

^-?[1-9]\d*$

Solution 4 - Regex

This works for 1900 to 2099:

/(?:(?:19|20)[0-9]{2})/

Solution 5 - Regex

Building on @r92 answer, for years 1970-2019:

(19[789]\d|20[01]\d)

Solution 6 - Regex

To test a year in a string which contains other words along with the year you can use the following regex: \b\d{4}\b

Solution 7 - Regex

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:

Solution 8 - Regex

Use;

^(19|[2-9][0-9])\d{2}$ 

for years 1900 - 9999.

No need to worry for 9999 and onwards - A.I. will be doing all programming by then !!! Hehehehe

You can test your regex at https://regex101.com/

Also more info about non-capturing groups ( mentioned in one the comments above ) here http://www.manifold.net/doc/radian/why_do_non-capture_groups_exist_.htm

Solution 9 - Regex

you can go with sth like [^-]\d{4}$: you prevent the minus sign - to be before your 4 digits.
you can also use ^\d{4}$ with ^ to catch the beginning of the string. It depends on your scenario actually...

Solution 10 - Regex

You could convert your integer into a string. As the minus sign will not match the digits, you will have no negative years.

Solution 11 - Regex

I use this regex in Java ^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/](19|[2-9][0-9])[0-9]{2}$

Works from 1900 to 9999

Solution 12 - Regex

/^\d{4}$/ This will check if a string consists of only 4 numbers. In this scenario, to input a year 989, you can give 0989 instead.

Solution 13 - Regex

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})$

Solution 14 - Regex

You can also use this one.

([0-2][0-9]|3[0-1])\/([0-1][0-2])\/(19[789]\d|20[01]\d)

Solution 15 - Regex

In my case I wanted to match a string which ends with a year (4 digits) like this for example:

Oct 2020
Nov 2020
Dec 2020
Jan 2021

It'll return true with this one:

var sheetName = 'Jan 2021';
var yearRegex = new RegExp("\b\d{4}$");
var isMonthSheet = yearRegex.test(sheetName);
Logger.log('isMonthSheet = ' + isMonthSheet);

The code above is used in Apps Script.

Here's the link to test the Regex above: https://regex101.com/r/SzYQLN/1

Solution 16 - Regex

> You can try the following to capture valid year from a string:

.*(19\d{2}|20\d{2}).*

Solution 17 - Regex

Works from 1950 to 2099 and value is an integer with 4 characters

^(?=.*?(19[56789]|20\d{2}).*)\d{4}$

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRanhiru Jude CoorayView Question on Stackoverflow
Solution 1 - Regexr92View Answer on Stackoverflow
Solution 2 - RegexcodaddictView Answer on Stackoverflow
Solution 3 - RegexsferikView Answer on Stackoverflow
Solution 4 - RegexjonschlinkertView Answer on Stackoverflow
Solution 5 - RegexRenaudView Answer on Stackoverflow
Solution 6 - RegexDhyeyView Answer on Stackoverflow
Solution 7 - RegexAdil AliyevView Answer on Stackoverflow
Solution 8 - RegexMarcoZenView Answer on Stackoverflow
Solution 9 - RegexpierrozView Answer on Stackoverflow
Solution 10 - RegexComradinView Answer on Stackoverflow
Solution 11 - RegexR2RivsView Answer on Stackoverflow
Solution 12 - RegexDayzView Answer on Stackoverflow
Solution 13 - RegexBenjamin GoodacreView Answer on Stackoverflow
Solution 14 - RegexRanjeet ChouhanView Answer on Stackoverflow
Solution 15 - RegexLeniel MaccaferriView Answer on Stackoverflow
Solution 16 - RegexgreenieView Answer on Stackoverflow
Solution 17 - RegexАлина ПчельниковаView Answer on Stackoverflow