File & Pathflags: m
.env File Key-Value Line
Parse KEY=value lines from .env config files, handling quoted values and trailing comments.
Try it in RegexPro →Available in
Pattern
regexengine-agnostic
^([A-Z_][A-Z0-9_]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^#\s]*))(?:\s*#.*)?$ (flags: m)Raw source: ^([A-Z_][A-Z0-9_]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^#\s]*))(?:\s*#.*)?$
How it works
([A-Z_][A-Z0-9_]*) captures the variable name (uppercase + underscores by convention). The value alternation supports double-quoted, single-quoted, and unquoted values. (?:\s*#.*)? allows an optional inline comment. The m flag lets ^ and $ match individual lines in a multi-line file.
Examples
Input
DATABASE_URL=postgres://localhost/dbMatches
DATABASE_URL=postgres://localhost/db
Input
API_KEY="sk_live_abc123" # productionMatches
API_KEY="sk_live_abc123" # production
Input
lowercase=badNo match
—Common use cases
- •Linting and validating .env files in CI
- •Migrating dotenv configs between environments
- •Generating documentation from .env.example
- •Detecting committed secrets in PR diffs