DD
DevDash

Regex for snake_case Word

Regex Pattern

^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$

snake_case identifier (lowercase with underscores)

Quick Answer

The regex pattern for snake_case word is `^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$`. snake_case identifier (lowercase with underscores). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
my_variable✓ Matches
first_name✓ Matches
get_data_from_api✓ Matches
myVariable✗ No match
My_Variable✗ No match
single✗ No match
UPPER_CASE✗ No match

Code Examples

javascript

const regex = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$/;
const isValid = regex.test(value);

python

import re
pattern = r'^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$/', $value)) {
    echo "valid";
}

java

String pattern = "^[a-z][a-z0-9]*(?:_[a-z0-9]+)+$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.