Pattern library / Validation
Snake Case Regex
Python modules, env vars and database columns share one naming law: lowercase_with_underscores. The pattern enforces it end to end — no leading digit, no double underscore, no uppercase sneaking in — and the notes contrast it with its siblings (kebab, camel) so you pick the right law for the right codebase.
How the pattern works
^[a-z]— the first character is a letter: one constraint that keeps years, counters and version numbers out of the first slot.(_[a-z0-9]+)*— every underscore must be sandwiched between segments; that structurally rejectsdouble__, trailing_and leading_.- Siblings: kebab-case
^[a-z]+(-[a-z0-9]+)*$(the URL slug rule), camelCase^[a-z]+([A-Z][a-z0-9]*)*$, SCREAMING_SNAKE^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$. - Allow leading underscore for private names (
_internal): prepend_?— deliberate, not default.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
snake_case | Match | canonical form | Try → |
my_var2 | Match | digits allowed after the first character | Try → |
v2_api_key | Match | digit right after the letter is fine | Try → |
2_cool | No match | cannot start with a digit | Try → |
double__underscore | No match | empty segment between underscores | Try → |
kebab-case | No match | hyphens belong to kebab-case | Try → |
Where to use it
JavaScript
const SNAKE = /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/;
SNAKE.test('api_key_v2'); // true
SNAKE.test('API_KEY'); // false
Python (validate or convert)
import re
SNAKE = r'^[a-z][a-z0-9]*(_[a-z0-9]+)*$'
assert SNAKE.fullmatch('user_id_2')
name = re.sub(r'[^a-z0-9]+', '_', title.lower()).strip('_') # convert first
BigQuery (RE2)
WHERE REGEXP_CONTAINS(column_name, r'^[a-z][a-z0-9]*(_[a-z0-9]+)*$')
Common mistakes
- The quick
^[a-z0-9_]+$accepts2_cool,a__banda_— all invalid under strict style rules. - Existing data with sentinel suffixes (
order_999is fine,999_orderis not) fails on purpose — decide whether your code validates input or normalizes it first. - Unicode identifiers (
naïve_flag) need\p{L}classes and JavaScript'suflag — the ASCII form here is RE2-portable and matches most language specs anyway.
FAQ
How do I convert any string into snake_case?
Lowercase, replace every run of non [a-z0-9] with a single underscore, trim edges: s.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '') — then validate with this pattern.
What is the difference between snake_case and kebab-case?
snake_case uses underscores (user_id); kebab-case uses hyphens (user-id). Identical segment rules, one character apart — kebab is the URL convention covered by the slug pattern.
Why can't a snake_case name start with a digit?
Most languages forbid it (Python: 1var is a syntax error). The ^[a-z] anchor enforces that; drop it only if your field stores free-form keys rather than identifiers.
Related patterns
Slug Regex — Match lowercase-hyphen URL Slugs · SemVer Regex — Match Semantic Versioning (2.0.0) · Email Validation Regex — Practical Pattern to Test
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.