Regular Expressions 101

Email validator with 2 groups (name and domain)

Open Workspace
  • PCRE2 (PHP)
  • Match
0

Regular Expression

/
^((?!\.)(?!.*\.\.)[\w!#$%&'*+\/=?^`{|}~.-]+(?<!\.))@([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])?)+)$
/
gm

Description

Core principles Covers ~99% of practical use cases Returns exactly 2 capturing groups (username and domain) Balances between RFC 5322 compliance and practicality

āœ… Accepts: Basic email addresses Special characters: `! # $ % & ' * + / = ? ^ \ { | } ~ `` Dots, hyphens, underscores in username Hyphens and underscores in domain Multi-level domains (e.g., .co.uk) Username starting with _ or - Username ending with special characters (e.g., +)

āŒ Rejects: Username starting/ending with a dot Double dots in username or domain Missing TLD Domain starting/ending with a hyphen Spaces, quotes, parentheses Multiple @ signs IP addresses in square brackets

Submitted by gh/barabasz