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")

Frequently asked questions

What is the regex for snake_case identifier?

The pattern \b[a-z][a-z0-9]*(?:_[a-z0-9]+)+\b matches snake_case identifier. Starts with a lowercase letter, then one or more underscore-separated lowercase/digit groups. Requires at least one underscore to distinguish from plain words.

What does this snake_case identifier pattern match?

It matches strings like user_name, first_name_2, get_user_by_id, but rejects userName (no underscore), USER_NAME (uppercase).

How do I use this pattern in JavaScript, Python, Go, Java?

Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.

Related patterns

Try this pattern in a tool

Try any pattern live in the Regex Tester