Validation
Snake Case Identifier Regex
Database columns, Python variables, environment-style config keys and analytics event names are often written in snake_case, for example order_total or page_view_2026. This pattern requires a lowercase letter first, then lowercase letters and digits, with single underscores separating the parts. It rejects capital letters, spaces, hyphens, leading or trailing underscores and double underscores, so every name ends up in one predictable style. Use it in a naming linter, in an admin form where people create custom fields, or before generating code from user-supplied names, where an odd character could break the output.
Pattern (JavaScript)
/^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/Starts at the start of the line/string, then a lowercase letter, then one of the characters a-z0-9, zero or more times, then (the character "_", then one of the characters a-z0-9, one or more times), zero or more times, then the end of the line/string.
Examples
Matches
- ✓ order_total
- ✓ page_view_2026
- ✓ name
Does not match
- ✗ OrderTotal
- ✗ double__underscore
- ✗ _leading
Try it
0 matches (JavaScript engine)
order_total
page_view_2026
name
OrderTotal
double__underscore
_leadingCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/g;
const text = "order_total";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$')
text = 'order_total'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/';
$text = 'order_total';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$");
String text = "order_total";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$`)
text := "order_total"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$");
var text = "order_total";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$").unwrap();
let text = "order_total";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/
text = 'order_total'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why must the name start with a letter?
- Most programming languages and databases do not allow identifiers that start with a digit. Requiring a letter first keeps names usable as variables and column names.
- Why are double underscores rejected?
- Each underscore must be followed by at least one letter or digit. In Python, double underscores also have a special meaning, so avoiding them prevents confusion.
- How do I check SCREAMING_SNAKE_CASE constants instead?
- Use the same shape with uppercase letters: ^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$. Keep the flags empty so the case rule is enforced.