DD
DevDash

Regex for Environment Variable Name

Regex Pattern

^[A-Z_][A-Z0-9_]*$

Standard environment variable name (uppercase with underscores)

Quick Answer

The regex pattern for environment variable name is `^[A-Z_][A-Z0-9_]*$`. Standard environment variable name (uppercase with underscores). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
PATH✓ Matches
HOME✓ Matches
DATABASE_URL✓ Matches
NODE_ENV✓ Matches
_PRIVATE✓ Matches
path✗ No match
my-var✗ No match
123VAR✗ No match
has space✗ 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

Want API access + no ads? Pro coming soon.