Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^[a-z][a-z0-9_]*$

This regex matches strings that start with a lowercase letter and are followed by zero or more lowercase letters, digits, or underscores. It ensures the entire string conforms to this pattern from start to end.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string '' (at start of 'abc123')
[a-z] Character class: matches any single lowercase letter (a-z) 'a' (in 'abc123')
[a-z0-9_] Character class: matches any single lowercase letter, digit, or underscore 'b', '1', '_' (in 'abc123_')
* Quantifier: matches zero or more occurrences of the preceding element ([a-z0-9_]) '' (after 'a' in 'a'), 'bc123_' (in 'abc123_')
$ Anchor: asserts the position at the end of the string '' (at end of 'abc123')

Matches

  • a
  • abc
  • a1
  • a_b
  • abc123_
  • z
  • test_string_123

Does not match

  • A
  • 1abc
  • _abc
  • abc!
  • abc def
  • a b
  • a b
  • a b

Common pitfalls

  • Forgetting the ^ anchor allows strings to start with invalid characters (e.g., '1abc' would match without ^).
  • Omitting the $ anchor allows strings to end with invalid characters (e.g., 'abc!' would match without $).
  • Assuming this matches empty strings (it does not, as [a-z] requires at least one character).
  • Confusing [a-z] with [A-Za-z] if case-insensitive matching is needed.
  • Overlooking that * allows zero occurrences, but the first [a-z] requires at least one character.

Performance notes

This pattern is efficient with no catastrophic backtracking risk due to the use of anchors and simple quantifiers. The character classes are straightforward and do not involve nested quantifiers. Performance is linear (O(n)) relative to the input string length.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this regex match empty strings?

No, because the first token [a-z] requires at least one lowercase letter, and the * quantifier allows zero or more of the following characters but does not compensate for the initial requirement.

How can I make this case-insensitive?

Use the (?i) flag at the start of the pattern in Python: r'(?i)^[a-z][a-z0-9_]*$' or compile the regex with re.IGNORECASE.

Can this regex match strings with spaces?

No, because the character class [a-z0-9_] does not include whitespace characters.

What is the minimum and maximum length of strings this regex matches?

Minimum length is 1 (e.g., 'a'), and maximum length is unbounded (limited only by string length constraints in Python).