REGEXVAULTv2.0
Web & Network/HTTP
Verified Safe

Accept-Language Header Value Regex for Go

/^[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 Go. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.

Go Implementation

Go
// Accept-Language Header Value
// ReDoS-safe | RegexVault — Web & Network > HTTP

package validation

import "regexp"

var acceptlanguageHeaderValueRe = regexp.MustCompile(`^[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})?))?)*$`)

func ValidateAcceptlanguageHeaderValue(s string) bool {
    return acceptlanguageHeaderValueRe.MatchString(s)
}

// Example
// fmt.Println(ValidateAcceptlanguageHeaderValue("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 Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. 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