DevKits

Code

Match a snake_case identifier

Match identifiers like user_name or first_name_2.

Pattern

Regular expression

\b[a-z][a-z0-9]*(?:_[a-z0-9]+)+\b

How it works

Starts with a lowercase letter, then one or more underscore-separated lowercase/digit groups. Requires at least one underscore to distinguish from plain words.

Matches

  • user_name
  • first_name_2
  • get_user_by_id

Non-matches

  • userName (no underscore)
  • USER_NAME (uppercase)

Language snippets

JavaScript

const re = /\b[a-z][a-z0-9]*(?:_[a-z0-9]+)+\b/g;

Python

re.findall(r"\b[a-z][a-z0-9]*(?:_[a-z0-9]+)+\b", text)

Go

re := regexp.MustCompile(`\b[a-z][a-z0-9]*(?:_[a-z0-9]+)+\b`)

Java

Pattern.compile("\\b[a-z][a-z0-9]*(?:_[a-z0-9]+)+\\b")

Related patterns

Try any pattern live in the Regex Tester