BCP 47 Language Tag in GO
Validate BCP 47 language tags like `en`, `en-US`, `zh-Hant-TW`, or `pt-BR`.
Try it in the GO tester →Pattern
regexGO
^[a-zA-Z]{2,3}(?:-[A-Za-z0-9]{2,8})*$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-zA-Z]{2,3}(?:-[A-Za-z0-9]{2,8})*$`)
input := `en`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
^[a-zA-Z]{2,3} matches the primary 2- or 3-letter language subtag. (?:-[A-Za-z0-9]{2,8})* matches subsequent subtags (script, region, variant) joined with hyphens. Each subtag is 2–8 alphanumerics. This validates STRUCTURE — for spec-compliant validation use a real BCP 47 parser.
Examples
Input
enMatches
en
Input
zh-Hant-TWMatches
zh-Hant-TW
Input
ENGLISHNo match
—