Jude Cooray
Jude Cooray

Reputation: 19862

Regular Expression Match to test for a valid year

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?

Upvotes: 119

Views: 218923

Answers (17)

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

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

Upvotes: -1

greenie
greenie

Reputation: 444

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

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

Upvotes: 0

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102408

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

Upvotes: 0

Ranjeet Chouhan
Ranjeet Chouhan

Reputation: 694

You can also use this one.

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

Upvotes: 0

BenValjean
BenValjean

Reputation: 407

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

Upvotes: 0

MarcoZen
MarcoZen

Reputation: 1663

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

Upvotes: 2

Adil Aliyev
Adil Aliyev

Reputation: 1169

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}

Upvotes: 3

Dhyey
Dhyey

Reputation: 4335

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

Upvotes: 4

Dayz
Dayz

Reputation: 269

/^\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.

Upvotes: 1

jonschlinkert
jonschlinkert

Reputation: 11007

This works for 1900 to 2099:

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

Upvotes: 13

R2Rivs
R2Rivs

Reputation: 1

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

Upvotes: 0

Comradin
Comradin

Reputation: 202

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

Upvotes: 0

sferik
sferik

Reputation: 1835

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*$

Upvotes: 22

Renaud
Renaud

Reputation: 16501

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

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

Upvotes: 6

r92
r92

Reputation: 2813

Years from 1000 to 2999

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

For 1900-2099

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

Upvotes: 229

pierroz
pierroz

Reputation: 7870

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...

Upvotes: 1

codaddict
codaddict

Reputation: 455020

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.

Upvotes: 101

Related Questions