Regex for Dotenv Line
Regex Pattern
^[A-Z_][A-Z0-9_]*=.*$.env file line (KEY=value format)
Quick Answer
The regex pattern for dotenv line is `^[A-Z_][A-Z0-9_]*=.*$`. .env file line (KEY=value format). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| DATABASE_URL=postgres://localhost/db | ✓ Matches |
| NODE_ENV=production | ✓ Matches |
| SECRET_KEY=abc123 | ✓ Matches |
| # comment | ✗ No match |
| lowercase=bad | ✗ No match |
| =no-key | ✗ No match |
Code Examples
javascript
const regex = /^[A-Z_][A-Z0-9_]*=.*$/; const isValid = regex.test(value);
python
import re
pattern = r'^[A-Z_][A-Z0-9_]*=.*$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^[A-Z_][A-Z0-9_]*=.*$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^[A-Z_][A-Z0-9_]*=.*$/', $value)) {
echo "valid";
}java
String pattern = "^[A-Z_][A-Z0-9_]*=.*$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
JavaScript Variable Name
Valid JavaScript variable/identifier name
Python Variable Name
Valid Python identifier name
CSS Class Name
Valid CSS class name (starts with letter, underscore, or hyphen)
CSS ID Selector
CSS ID selector starting with #
JSON Key
JSON object key (quoted string followed by colon)
Semantic Version (Strict)
Strict semantic versioning (MAJOR.MINOR.PATCH)