REGEXVAULTv2.0
Finance/Card Numbers
Verified Safe

Card Expiry Date (MM/YY or MM/YYYY) Regex for Java

/^(0[1-9]|1[0-2])/(?:20[0-9]{2}|[2-9][0-9])$/

What this pattern does

This page provides a well-structured, multi-part regular expression for matching card expiry date (mm/yy or mm/yyyy), ported and verified for Java. Financial data validation has zero tolerance for false negatives — a missed invalid entry can corrupt downstream calculations. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.

Java Implementation

Java
// Card Expiry Date (MM/YY or MM/YYYY)
// ReDoS-safe | RegexVault — Finance > Card Numbers

import java.util.regex.Pattern;

public class CardExpiryDateMmyyOrMmyyyyValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^(0[1-9]|1[0-2])/(?:20[0-9]{2}|[2-9][0-9])$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("01/24")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
01/2400/24
12/2913/24
06/20251/24
01/209901/24/00
01-24
01/9
01/199

When to use this pattern

This pattern is drawn from the Finance > Card Numbers category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

A card expires at the end of the expiry month (not the 1st). 06/24 is valid until June 30, 2024 23:59:59. Do not reject on the expiry month's first day.

Technical Notes

Capture group 1: month (01-12), group 2: year (2-digit or 4-digit). 2-digit years in the range 20-99 map to 2020-2099. Cards expired but still within the grace period may still be valid — check against today's date at application level.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern