Regular Expressions 101

Community Patterns

Your search did not match anything

Community Library Entry

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^((?!\.)(?!.*\.\.)[\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 - 16 days ago