Go (RE2)

HTTP Content-Type Header in GO

Parse the value of an HTTP Content-Type header, capturing the media type and optional charset.

Try it in the GO tester →

Pattern

regexGO
Content-Type:\s*([a-z]+\/[\w.+\-]+)(?:;\s*charset=([\w\-]+))?   (flags: gi)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?i)Content-Type:\s*([a-z]+\/[\w.+\-]+)(?:;\s*charset=([\w\-]+))?`)
	input := `Content-Type: text/html; charset=utf-8`
	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

Content-Type:\s* matches the header name and required whitespace. ([a-z]+\/[\w.+\-]+) captures the type/subtype (the i flag makes this case-insensitive). (?:;\s*charset=([\w\-]+))? optionally captures a charset parameter — UTF-8, iso-8859-1, etc.

Examples

Input

Content-Type: text/html; charset=utf-8

Matches

  • Content-Type: text/html; charset=utf-8

Input

content-type: application/json

Matches

  • content-type: application/json

Input

Authorization: Basic xyz

No match

Same pattern, other engines

← Back to HTTP Content-Type Header overview (all engines)