Pattern library / Extract & Replace
Regex to Extract the Value from a key=value Pair
Log lines, env files, query strings and config all speak key=value — and pulling one value out never needs a parser. Group 1 captures the key, group 2 the value: quoted values keep their spaces, empty values still match, and everything up to the next whitespace is yours in one pass. Valid RE2, so the same pattern runs in GA4, BigQuery, Go and Rust — test any line below in the tester first.
How the pattern works
([\w.-]+)— the key: letters, digits, underscore, dot and hyphen, one or more. That covers env vars (PORT), dotted config keys (server.port), log fields (user_id) and query params (q) — and because it demands at least one character, a bare=never matches.=— exactly one separator with no spaces around it.PORT=3000matches,PORT = 3000does not (see pitfalls for the loosened form).("[^"]*"|[^\s]*)— the value, two branches tried in order: a quoted branch that runs to the closing quote so spaces survive, or an unquoted run that stops at the next whitespace and may be empty — soretries=still matches.- Unanchored on purpose: the pattern finds pairs anywhere in a line, so one pass works over a whole log file. Plain RE2 — no lookaround, no backreferences.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
level=info msg="disk almost full" | Match | a log line: the first pair matches, quoted value included | Try → |
PORT=3000 | Match | an env var — the whole string is one pair | Try → |
GET /api/search?q=regex&page=2 HTTP/1.1 | Match | an access log line: q= is found even behind the ? | Try → |
user_id=42 retries= | Match | the second pair has an empty value — the value branch may match nothing | Try → |
cache hit: nothing to parse here | No match | no = sign, so there is no pair to find | Try → |
=missing-key | No match | the key class needs at least one character before the = (see pitfalls) | Try → |
Where to use it
JavaScript
const pairs = Object.fromEntries(
[...line.matchAll(/([\w.-]+)=("[^"]*"|[^\s]*)/g)]
.map(([, k, v]) => [k, v.replace(/^"|"$/g, '')])
);
// 'level=info msg="disk almost full"' → { level: 'info', msg: 'disk almost full' }
Python
import re
pairs = dict(re.findall(r'([\w.-]+)=("[^"]*"|[^\s]*)', line))
pairs = {k: v.strip('"') for k, v in pairs.items()}
# {'level': 'info', 'msg': 'disk almost full'}
BigQuery / GA4 (RE2)
SELECT REGEXP_EXTRACT(line, r'[\w.-]+=("[^"]*"|[^\s]*)') AS first_value, -- group 1 = value of the first pair
REGEXP_EXTRACT(line, r'status=("[^"]*"|[^\s]*)') AS status -- pin one key: group 1 = its value
FROM logs
Common mistakes
- Unquoted values stop at the first space: in
msg=hello worldthe value comes back as justhello. Quote the value (msg="hello world"works as-is) or, for URL-encoded query values, stop at the ampersand instead:([\w.-]+)=("[^"]*"|[^&\s]*). - The generic pattern grabs the first pair it meets, whatever the key. To read ONE key, pin it into the pattern —
status=("[^"]*"|[^\s]*)— so group 1 is that value and a line with twenty pairs can never hand you the wrong one. - Spaces around the separator break the match:
PORT = 3000has=and never hits. Loosen just the separator when you meet that format:([\w.-]+)\s*=\s*("[^"]*"|[^\s]*).
FAQ
How do I extract the value of just ONE key?
Pin the key: timeout=([^\s]+). With the literal key in front, group 1 becomes the value itself — the same in JavaScript (line.match(...)), Python (re.search(...)) and BigQuery (REGEXP_EXTRACT). Use the generic pattern to scan a line, the pinned one to read a field.
Why does the value swallow the rest of my query string?
Unquoted values run to whitespace, so in ?q=regex&page=2 the value of q comes back as regex&page=2. Stop at the ampersand: ([\w.-]+)=("[^"]*"|[^&\s]*). When you need the whole string first, match it with the query string regex, then extract.
Can I use this inside Google Analytics 4?
Yes — it is plain RE2. BigQuery REGEXP_EXTRACT returns the capture directly — see the REGEXP_EXTRACT vs REGEXP_CONTAINS guide for NULL vs empty-string semantics, and GA4's RE2 engine accepts the pattern too, but GA4 filters only match, they never return a substring: extract in BigQuery or your ETL and compare against the result. The match type still decides whether it must cover the whole value.
Related patterns
Log Level Regex — Match INFO / WARN / ERROR Lines · Query String Regex — Match ?key=value — Test & Copy · JSON Key Regex — Match Property Names — Test & Copy
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.