Date/Time
Year 1900 to 2099 Regex
Many forms and documents only deal with years in the twentieth and twenty-first centuries, such as birth years, copyright notices and publication dates. This pattern finds four-digit numbers from 1900 to 2099 standing on their own, using word boundaries so that digits inside longer numbers such as 12026 or 20261 are ignored. It is useful for extracting years from titles, filtering spreadsheet columns and quick checks on date fields. The honest limit is that any number in that range matches, including prices, quantities or codes that only look like years.
Pattern (JavaScript)
/\b(?:19|20)[0-9]{2}\b/Matches a word boundary, then (the text "19" OR the text "20"), then a digit, exactly 2 times, then a word boundary.
Examples
Matches
- ✓ in 1999
- ✓ 2026
- ✓ Copyright 2050
Does not match
- ✗ 1899
- ✗ 2100
- ✗ 12026
Try it
3 matches (JavaScript engine)
in 1999 2026 Copyright 2050 1899 2100 12026
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /\b(?:19|20)[0-9]{2}\b/g;
const text = "in 1999";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'\b(?:19|20)[0-9]{2}\b')
text = 'in 1999'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/\b(?:19|20)[0-9]{2}\b/';
$text = 'in 1999';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\b(?:19|20)[0-9]{2}\\b");
String text = "in 1999";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\b(?:19|20)[0-9]{2}\b`)
text := "in 1999"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"\b(?:19|20)[0-9]{2}\b");
var text = "in 1999";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"\b(?:19|20)[0-9]{2}\b").unwrap();
let text = "in 1999";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /\b(?:19|20)[0-9]{2}\b/
text = 'in 1999'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How do I accept only past years?
- Compare the matched value to the current year in code. A regex cannot know today's date, so changing ranges are better handled outside the pattern.
- Why do word boundaries matter here?
- Without them, a search would find 2026 inside 120265 or a long ID. The boundaries require the year to be its own token.
- How do I use this for a form field?
- Replace the \b boundaries with ^ and $ anchors so the field must contain only the year. Keep \b for searching inside longer text.
Related patterns
- Month Name\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Sep(?:t|tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\b
- Date DD/MM/YYYY^(0[1-9]|[12][0-9]|3[01])[/.-](0[1-9]|1[0-2])[/.-]([0-9]{4})$
- Date with Month Name^(0?[1-9]|[12][0-9]|3[01]) (?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|June?|July?|Aug(?:ust)?|Sep(?:t|tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?),? ([0-9]{4})$
- ISO 8601 Date (YYYY-MM-DD)^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$