Go (RE2)

Cookie Header Value in GO

Parse name=value pairs from an HTTP `Cookie:` header value.

Try it in the GO tester →

Pattern

regexGO
([^=;\s]+)=([^;]+)   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`([^=;\s]+)=([^;]+)`)
	input := `session=abc123; theme=dark; user_id=42`
	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

([^=;\s]+) captures the cookie name — characters that aren't equals, semicolon, or whitespace. = is the literal separator. ([^;]+) captures the value — everything up to the next semicolon, allowing spaces and special chars inside the value. Repeats globally to capture every cookie.

Examples

Input

session=abc123; theme=dark; user_id=42

Matches

  • session=abc123
  • theme=dark
  • user_id=42

Input

single=value

Matches

  • single=value

Input

no cookies

No match

Same pattern, other engines

← Back to Cookie Header Value overview (all engines)