Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^[\w\-\.]+@([\w\-]+\.)+[\w\-]{2,4}$

Matches a basic email address format with a local part, domain, and top-level domain (TLD) of 2 to 4 characters. The domain may contain subdomains.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of 'user@example.com'
[\w\-\.]+ Character class matching one or more word characters (letters, digits, underscore), hyphens, or dots 'user', 'user.name', 'user-name'
@ Literal '@' character '@' in 'user@example.com'
( Start of a non-capturing group (implicit in Python regex) Start of domain group in 'user@example.com'
[\w\-]+ Character class matching one or more word characters or hyphens 'example', 'sub-domain'
\. Literal '.' character (escaped due to special meaning in regex) '.' in 'example.com'
)+ Quantifier: one or more occurrences of the preceding group (domain part with subdomains) 'example.', 'sub.example.'
[\w\-]{2,4} Character class matching 2 to 4 word characters or hyphens (TLD) 'com', 'org', 'co.uk'
$ Anchor: asserts position at end of the string End of 'user@example.com'

Matches

  • user@example.com
  • user.name@sub.example.org
  • user-name@domain.co.uk
  • a@b.cd
  • 123@456.789

Does not match

  • user@.com
  • user@example..com
  • user@example.c
  • user@example.comm
  • user@example.com.
  • @example.com
  • user@
  • user@example

Common pitfalls

  • Does not validate the full RFC 5322 email standard (e.g., quotes, comments, or special characters in local part are not supported).
  • Allows invalid TLDs like 'co.uk' (4 chars) but also invalid ones like 'commm' (5 chars).
  • Does not enforce a minimum length for the local part or domain.
  • Hyphens at the start/end of domain parts are technically allowed but may be invalid in practice (e.g., 'user@-example.com').
  • Overly simplistic for real-world email validation.

Performance notes

No catastrophic backtracking risk due to greedy quantifiers and no nested quantifiers. However, the pattern is not optimized for performance in large-scale validation due to the lack of strict length constraints and character restrictions.

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 does the pattern allow hyphens and dots in the local part?

The pattern uses [\w\-\.]+ for the local part, which allows hyphens and dots. This is a simplification and does not fully comply with RFC 5322, which has stricter rules for the local part.

Can this pattern match internationalized email addresses (e.g., with non-ASCII characters)?

No. The pattern only matches ASCII characters. For internationalized emails, Unicode properties or IDN (Internationalized Domain Names) support would be required.

Why is the TLD limited to 2-4 characters?

The pattern assumes a traditional TLD length (e.g., .com, .org). Modern TLDs can be longer (e.g., .photography), and this pattern would fail to match them.

Is this pattern suitable for production use?

No. This pattern is overly simplistic and does not cover many edge cases or real-world email validation requirements. Use a dedicated email validation library or RFC-compliant regex.