Regex tester

Live matches, capture groups and a replace preview, with a pattern cheat sheet.

/ /gi
Matches
3

Match details

#IndexMatchGroups
18ada@example.com$1: ada$2: example.com
227grace@navy.mil$1: grace$2: navy.mil
397alan@bletchley.uk$1: alan$2: bletchley.uk

Replace

$1 for a capture group, $& for the whole match

Pattern library

Cheat sheet

. Any character except a newline
\d \w \s Digit, word character, whitespace
\D \W \S The negation of each
[abc] Any one of a, b or c
[^abc] Any character except those
[a-z] A range
* + ? Zero or more, one or more, optional
{2,5} Between two and five times
*? +? Lazy — match as little as possible
^ $ Start and end of string (or line with m)
\b Word boundary
(...) Capture group
(?:...) Group without capturing
(?<name>...) Named capture group
a|b Either a or b
(?=...) Positive lookahead
(?!...) Negative lookahead
(?<=...) Positive lookbehind

How it works

A regular expression describes a pattern of characters. The engine walks your test string looking for the shortest path through the pattern that succeeds — which is why understanding greediness matters more than memorising syntax.

Greedy versus lazy

<.*> on <a>text</a> matches the whole string, because * takes as much as it can and then backtracks only enough to succeed. Adding ? makes it lazy: <.*?> matches just <a>. This single distinction accounts for a large share of “why does my regex match too much”.

Catastrophic backtracking

Nested quantifiers like (a+)+b can take exponential time on a non-matching input, because the engine tries every possible split. This is a real denial-of-service vector when patterns run on user input. Avoid nesting quantifiers, or use an engine like RE2 that guarantees linear time.

Do not parse HTML with regex

HTML is not a regular language — nesting is unbounded, so no regular expression can match balanced tags correctly. Use a parser. The pattern in the library above is for finding tags in plain text, not for understanding a document.

Email is harder than it looks

The fully RFC 5322 compliant email pattern is several thousand characters and still accepts addresses no mail server will deliver to. The pragmatic pattern above catches typos; the only real validation is sending a confirmation message.