Regular Expressions 101

Community Patterns

Email validation regex

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(?!.*@.*@)(?!.*\.\.)(^[a-zA-Z0-9._%+-]{1,64}@)([a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:\.[a-zA-Z]{2,255})?)$
/
gm

Description

Email validation

Explanation

  1. ^(?!.@.@) Asserts that the string does not contain two consecutive “@” symbols.

  2. ** (?!.*..)**
    Ensures that there are no consecutive periods (“.”) in the string.

  3. ^[a-zA-Z0-9._%+-]{1,64}@ Matches an email address with a maximum length of 64 characters before the “@” symbol. It allows alphanumeric characters, dots, underscores, percent signs, plus signs, and hyphens.

  4. ([a-zA-Z0-9.-]+.[a-zA-Z]{2,}(?:.[a-zA-Z]{2,255})?)$
    Captures the domain part of the email address. It allows alphanumeric characters, dots, and hyphens.

  5. (?:.[a-zA-Z]{2,255})? This group allows for optional subdomains (e.g., “.foo.bar”).

Includes

  • valid local part
  • Hostpart
  • valid TLD
  • optional SLD
  • unique @
  • No double dots > ..

If you have any improvements or issues: Cxbersxnic - Github

Submitted by Cxbersxnic - 7 days ago