DD
DevDash

Regex for Year (4-digit)

Regex Pattern

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

Four-digit year between 1900 and 2099

Quick Answer

The regex pattern for year (4-digit) is `^(?:19|20)\d{2}$`. Four-digit year between 1900 and 2099. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
2026✓ Matches
1999✓ Matches
2000✓ Matches
1900✓ Matches
26✗ No match
99✗ No match
2100✗ No match
1899✗ No match

Code Examples

javascript

const regex = /^(?:19|20)\d{2}$/;
const isValid = regex.test(value);

python

import re
pattern = r'^(?:19|20)\d{2}$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^(?:19|20)\d{2}$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^(?:19|20)\d{2}$/', $value)) {
    echo "valid";
}

java

String pattern = "^(?:19|20)\\d{2}$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.