PHP regex for email addresses

This pattern accepts an address made of a local part, an @, and a domain with at least two labels separated by dots: [email protected], [email protected]. It refuses an empty local part or domain, a part that starts or ends with a dot, two dots in a row in the domain, and any space.

It is a simplified form of the address RFC 5322 defines. A quoted local part ("john doe"@example.com), a domain written as a bare host name (user@localhost) or an IP literal in brackets (user@[127.0.0.1]) does not pass. The i flag lets capitals through everywhere.

/^[a-z0-9]([a-z0-9._%+-]*[a-z0-9])?@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/i

Try it. Open /^[a-z0-9]([a-z0-9._%+-]*[a-z0-9])?@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/i in the playground: the tree and the verdict sit on the same screen, and the Diagnostics tab shows what the linter says of it.

What it matches

✓ [email protected]
✓ [email protected]
✓ [email protected]
✓ [email protected]
✓ [email protected]  (capitals pass under the i flag)
✓ [email protected]  (two dots in a row pass in the local part)

What it rejects

✗ @example.com  (missing local part)
✗ user@  (missing domain)
✗ [email protected]  (empty domain part)
✗ [email protected]  (consecutive dots)
✗ user@exam ple.com  (space in domain)
✗ user@localhost  (no dot in the domain)

Why it is safe

  • The local part starts with a letter or a digit and allows dots, underscores, percent signs, plus signs and hyphens in the middle — so Gmail-style user+tag addresses pass.
  • The domain starts with a letter or a digit and allows hyphenated parts.
  • At least one dot-separated label follows the first label of the domain: the TLD.
  • The repeats nest, and the linter says so: regex.lint.quantifier.nested on the third group. The nesting is harmless here: every label of the domain starts after a dot and ends on a letter or a digit, so the engine reads a label one way and never re-reads a long input in many ways. The analyzer’s verdict is safe (proven).

How to validate an email in PHP

preg_match() is all the check needs. The example also asks PHPRegex for its two answers on the pattern: whether it is valid, and whether a match on it can blow up in time. It uses the Regex facade of the php-regex/regex-toolkit package, whose install command the cookbook gives.

use PHPRegex\Toolkit\Regex;

$email = '[email protected]';
$pattern = '/^[a-z0-9]([a-z0-9._%+-]*[a-z0-9])?@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/i';

$regex = Regex::create();
$result = $regex->validate($pattern);
echo $result->isValid ? 'Valid pattern' : 'Invalid pattern', PHP_EOL;  // Valid pattern

$isMatch = preg_match($pattern, $email) === 1;
echo $isMatch ? 'Matches' : 'Does not match', PHP_EOL;                 // Matches

// ReDoS check
$analysis = Regex::create()->redos($pattern);
echo $analysis->headline(), PHP_EOL;                                   // safe (proven)

Back to the cookbook for the other fourteen recipes, and for how to build a pattern of your own.

Edit on GitHub Feedback