Python (re)

Cookie Header Value in PY

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

Try it in the PY tester →

Pattern

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

Python (re) code

pyPython
import re

pattern = re.compile(r"([^=;\s]+)=([^;]+)")
input_text = "session=abc123; theme=dark; user_id=42"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

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)