Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^[a-z0-9]+(?:[a-z0-9\-]+[a-z0-9]+)*\.?[a-z0-9\-]*[a-z0-9]$

Matches a string that starts and ends with alphanumeric characters, allowing hyphens in the middle, with optional single dot separators and no leading/trailing hyphens or dots.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: start of string Start of 'abc123'
[a-z0-9] Character class: lowercase letters or digits 'a', '1'
+ Quantifier: one or more of the preceding element 'abc123'
(?: Non-capturing group start Grouping for repetition
[a-z0-9\-] Character class: lowercase letters, digits, or hyphen 'a', '1', '-'
+ Quantifier: one or more of the preceding element 'a-1b'
[a-z0-9] Character class: lowercase letters or digits 'a', '1'
) Non-capturing group end End of group
* Quantifier: zero or more of the preceding element Empty or 'a-1b'
\. Escaped literal dot '.'
? Quantifier: zero or one of the preceding element No dot or single dot
[a-z0-9\-] Character class: lowercase letters, digits, or hyphen 'a', '1', '-'
* Quantifier: zero or more of the preceding element Empty or 'a-1b'
[a-z0-9] Character class: lowercase letters or digits 'a', '1'
$ Anchor: end of string End of 'abc123'

Matches

  • abc123
  • a-b-c123
  • abc.123
  • a1b2c3
  • a-b.c-d123
  • 123abc
  • a1-b2-c3
  • a.b.c123

Does not match

  • ABC123
  • -abc123
  • abc123-
  • abc..123
  • abc123.
  • .abc123
  • a--b123
  • a_b123

Common pitfalls

  • Forgetting to escape the dot (\.) which matches any character if not escaped
  • Assuming the pattern allows uppercase letters (it does not)
  • Not accounting for the strict start/end alphanumeric requirement
  • Overlooking that hyphens cannot be at the start or end of the string

Performance notes

The pattern is efficient due to the use of non-capturing groups and quantifiers. No catastrophic backtracking risk as the structure is linear with bounded repetition.

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

Why use (?:...) instead of (...)?

Non-capturing groups (?:...) are used for grouping without capturing, which improves performance by avoiding unnecessary capture overhead.

Can this pattern match domain names?

Yes, it can match valid domain name segments (e.g., 'sub-domain1.example'), but it does not validate full domain structures (e.g., TLDs or multiple dots).

Why is the last [a-z0-9] required?

To ensure the string does not end with a hyphen or dot, enforcing alphanumeric endings.