Logsflags: g
Logfmt Key-Value Pair
Parse key=value pairs from logfmt-style log lines, supporting both quoted and unquoted values.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("([a-zA-Z_][\\w.]*)=(\"[^\"]*\"|\\S+)", "g");
const input = "level=info msg=\"user logged in\" user_id=42";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"([a-zA-Z_][\\w.]*)=(\"[^\"]*\"|\\S+)")
input_text = "level=info msg=\"user logged in\" 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+.
Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`([a-zA-Z_][\w.]*)=("[^"]*"|\S+)`)
input := `level=info msg="user logged in" 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.
Pattern
regexengine-agnostic
([a-zA-Z_][\w.]*)=("[^"]*"|\S+) (flags: g)Raw source: ([a-zA-Z_][\w.]*)=("[^"]*"|\S+)
How it works
([a-zA-Z_][\w.]*) captures the key: starts with a letter or underscore, followed by word chars or dots. = is a literal separator. ("[^"]*"|\S+) captures the value: either a double-quoted string (allowing spaces inside) or an unquoted sequence of non-whitespace characters.
Examples
Input
level=info msg="user logged in" user_id=42Matches
level=infomsg="user logged in"user_id=42
Input
ts=2024-01-15T14:30:00Z status=200 latency=12msMatches
ts=2024-01-15T14:30:00Zstatus=200latency=12ms
Common use cases
- •Parsing logfmt output from Go services (standard library log/slog)
- •Extracting structured fields from Heroku and Fly.io log drains
- •Log aggregation pipelines (Vector, Fluentd, Filebeat)
- •Building log dashboards from structured text logs
Related patterns
.env File Key-Value Line
File & PathParse KEY=value lines from .env config files, handling quoted values and trailing comments.
JSON Key-Value Pair (Simple)
Text ProcessingExtract simple `"key": value` pairs from JSON-ish text (strings, numbers, booleans, null).
Log Level
LogsMatches standard log level keywords in log lines.
Nginx Error Log
LogsParses Nginx error log lines.