Advertisement

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.

The pattern
([\w.-]+)=("[^"]*"|[^\s]*)
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
level=info msg="disk almost full"Matcha log line: the first pair matches, quoted value includedTry →
PORT=3000Matchan env var — the whole string is one pairTry →
GET /api/search?q=regex&page=2 HTTP/1.1Matchan access log line: q= is found even behind the ?Try →
user_id=42 retries=Matchthe second pair has an empty value — the value branch may match nothingTry →
cache hit: nothing to parse hereNo matchno = sign, so there is no pair to findTry →
=missing-keyNo matchthe 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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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.

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.