REGEXVAULTv2.0
Web & Network/HTTP
Verified Safe

Accept-Language Header Value Regex for Python

/^[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})?))?)*$/i

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching accept-language header value, ported and verified for Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.

Python Implementation

Python
# Accept-Language Header Value
# ReDoS-safe | RegexVault — Web & Network > HTTP

import re

acceptlanguage_header_value_pattern = re.compile(r'^[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})?))?)*$')

def validate_acceptlanguage_header_value(value: str) -> bool:
    return bool(acceptlanguage_header_value_pattern.fullmatch(value))

# Example
print(validate_acceptlanguage_header_value("en"))  # True

Test Cases

Matches (Valid)
Rejects (Invalid)
enen;q=1.5
en-USen;q=-0.1
en-US,en;q=0.9,fr;q=0.8123
zh-CN,zh;q=0.9,en;q=0.8en-US, fr;q=abc
de-CH-x-phonebken-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 Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. 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