Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$

This regex validates an email address according to RFC 5322 standards, ensuring it has a local part, an @ symbol, and a domain part with valid subdomains and top-level domains.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts the position at the start of the string Start of string
[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+ Character class matching one or more allowed characters in the local part of the email (before @), including letters, digits, and special characters user.name+tag
@ Literal @ symbol separating local part and domain @
[a-zA-Z0-9] Character class matching a single alphanumeric character for the start of the domain part e
(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])? Non-capturing group for domain labels (max 63 chars, alphanumeric with hyphens allowed but not at start/end), optional example
(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)* Non-capturing group for subdomains and top-level domains, allowing zero or more occurrences of a dot followed by a domain label .com,.co.uk
$ Anchor: asserts the position at the end of the string End of string

Matches

  • user@example.com
  • first.last@sub.domain.co.uk
  • email+tag@domain.org
  • user_name@domain.net
  • a@b.c

Does not match

  • user@.com
  • user@domain..com
  • @domain.com
  • user@domain
  • user@domain.c
  • user@domain..com
  • user@-domain.com
  • user@domain-.com

Common pitfalls

  • Does not validate internationalized domain names (IDNs) or quoted strings in the local part
  • Does not enforce maximum total length of the email address (RFC 5321 recommends 254 chars)
  • Overly permissive with special characters in the local part that may not be accepted by all email providers
  • Does not check for consecutive dots in the local part or domain
  • Does not handle IP addresses in the domain part (e.g., user@[192.168.1.1])

Performance notes

The pattern is generally efficient due to the use of non-capturing groups and quantifiers with bounded repetition. However, very long domain labels (close to 63 characters) could lead to excessive backtracking in pathological cases. Anchoring at both start and end ensures the regex matches the entire string, preventing partial matches.

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 validate all valid email addresses according to RFC 5322?

No. While it covers many common cases, it does not handle all edge cases defined in RFC 5322, such as quoted strings with escaped characters or comments.

Why is the domain label limited to 63 characters?

RFC 1035 specifies that each label in a domain name can be up to 63 characters long, and this regex enforces that limit.

Can this regex be used for real-time email validation in user input forms?

It can be used as a basic check, but for production use, it is recommended to combine it with additional validation (e.g., DNS lookup, sending a verification email) as regex alone cannot guarantee an email address is deliverable.

Does this regex support Unicode characters in the local part or domain?

No. This regex only supports ASCII characters. For Unicode support, a more complex pattern or a dedicated library would be required.