Accept-Language Header Value Regex for Java
/^[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?(?:\s*,\s*[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?)*$/iWhat this pattern does
This page provides a comprehensive, battle-tested regular expression for matching accept-language header value, ported and verified for Java. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. 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
// Accept-Language Header Value
// ReDoS-safe | RegexVault — Web & Network > HTTP
import java.util.regex.Pattern;
public class AcceptlanguageHeaderValueValidator {
private static final Pattern PATTERN =
Pattern.compile("^[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\\.[0-9]{1,3})?|1(?:\\.0{1,3})?))?(?:\\s*,\\s*[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*(?:;q=(?:0(?:\\.[0-9]{1,3})?|1(?:\\.0{1,3})?))?)*$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("en")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
en | en;q=1.5 |
en-US | en;q=-0.1 |
en-US,en;q=0.9,fr;q=0.8 | 123 |
zh-CN,zh;q=0.9,en;q=0.8 | en-US, fr;q=abc |
de-CH-x-phonebk | en-US;;q=0.9 |
When to use this pattern
This pattern is drawn from the Web & Network > HTTP 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
An empty Accept-Language value is not valid — some clients send * (any language) as a wildcard, which this pattern does not match. Add \* as an additional alternative if needed.
Technical Notes
Quality factor (q) must be between 0 and 1 with up to 3 decimal places. Default q value is 1.0 when omitted. Used for content negotiation.
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