AI Answer Engine

Developer & tech

Regex Generator

A regular expression you can paste, plus the explanation of every token in it — because a pattern you cannot read is a bug waiting to happen.

Advertisement

Options

Your regular expression

/^[\w.+-]+@[\w-]+\.[\w.-]{2,}$/i

Anchored: the entire input must match. Case is ignored. Returns the first match only.

Piece by piece

[\w.+-]+
one or more letters, digits, dot, plus or hyphen — the local part
@
a literal at sign
[\w-]+
the domain label
\.[\w.-]{2,}
a dot followed by the top-level domain, two characters or more
^ … $
the whole string must match, not just part of it

Matches

  • jane.doe@example.com
  • sales+eu@shop.co.uk

Does not match

  • jane@localhost
  • @example.com

JavaScript

const re = /^[\w.+-]+@[\w-]+\.[\w.-]{2,}$/i;
re.test(input);

Python

import re
re.search(r"^[\w.+-]+@[\w-]+\.[\w.-]{2,}$", input, flags=re.IGNORECASE)

Using a regular expression safely

Validation patterns should be permissive. The email pattern here rejects obviously broken input rather than trying to implement the full specification — the only way to know an address works is to send a message to it.

Watch for catastrophic backtracking. Nested quantifiers such as (a+)+ can turn a twenty-character string into seconds of CPU time, which is a denial-of-service route when the pattern runs on user input. Keep patterns flat and test them against long inputs.

Never parse HTML, JSON or CSV with a regular expression. Those formats nest, and a pattern has no memory of how deep it is. Use a parser and save regex for flat, line-shaped text.