CVE Identifier Regex for Java
/^CVE-(19[6-9][0-9]|20[0-9]{2})-([0-9]{4,7})$/iWhat this pattern does
This page provides a well-structured, multi-part regular expression for matching cve identifier, ported and verified for Java. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. 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
// CVE Identifier
// ReDoS-safe | RegexVault — Security > Network Security
import java.util.regex.Pattern;
public class CveIdentifierValidator {
private static final Pattern PATTERN =
Pattern.compile("^CVE-(19[6-9][0-9]|20[0-9]{2})-([0-9]{4,7})$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("CVE-2021-44228")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
CVE-2021-44228 | CVE-2021-123 |
CVE-2024-0001 | CVE-21-44228 |
CVE-2014-0160 | CWE-2021-44228 |
CVE-1999-0001 | CVE-2021-1234ABCD |
CVE-2023-1234567 | — |
When to use this pattern
This pattern is drawn from the Security > Network Security 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
NNNNN can be longer than 4 digits for high-volume years (2023 had over 28000 CVEs). Ensure your pattern handles 5-7 digit sequences. CVE IDs with 7 digits appeared starting around 2014.
Technical Notes
CVE format: CVE-YYYY-NNNNN where YYYY is 1960-present year and NNNNN is at least 4 digits. Notable examples: CVE-2021-44228 (Log4Shell), CVE-2014-0160 (Heartbleed), CVE-2017-5638 (Apache Struts/Equifax breach). Year range starts 1960 (earliest MITRE entry).
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