mcquaim
mcquaim

Reputation: 179

Validate a 4-digit year using a range with regular expressions

I want to do something similar to what was required in this post: Regex Valid Year check

I would like to do something similar but only with years between 2011 & 2099?

Thanks, Mac

Upvotes: 1

Views: 8850

Answers (3)

Nicolas Meier
Nicolas Meier

Reputation: 108

Try this:

^20[1-9][0-9]$

^20 starts with 20 [1-9] followed by 1,2,3,4,5,6,7,8,9 [0-9] followed by 0,1,2,3,4,5,6,7,8,9

Edit: It will also accept 2010.

See this Answer by Ole V.V.

Upvotes: 0

Simo
Simo

Reputation: 195

Here is an example

^20((1[1-9])|([2-9][0-9]))$

it matches exactly from 2011 to 2099

Upvotes: 1

Anonymous
Anonymous

Reputation: 86286

My taste would be for parsing as an integer (Integer.parseInt()) and checking the bounds with <= or similar. But if you insist on the regular expression:

^20(1[1-9]|[2-9][0-9])$

The first case covers 2011–2019, the other 2020–2099. I have not tested.

Upvotes: 8

Related Questions