How to Test a Regular Expression
To test a regular expression, enter the pattern and paste representative sample text. Matches are highlighted in place and capture groups listed separately. Work up from a pattern that matches too much, tightening it until it matches only what you want. Test against text that should not match as well — a regex that never rejects anything is not finished.
Regex Tester
Test a regular expression and see every match highlighted.
The mistake almost everyone makes is testing only against input that should match. A pattern that correctly finds every email address in your sample but also matches "not-an-email@" is worse than useless in production, and you will not discover that from happy-path testing.
The second mistake is nesting quantifiers. A pattern like (a+)+$ looks harmless and will hang a CPU for minutes on a modest input, because the engine explores exponentially many ways to split the string before giving up.
Step by step
-
Paste realistic sample text
Use real data, including the awkward cases: empty lines, unusual punctuation, accented characters, and strings that look close to a match but should be rejected.
-
Start broad, then tighten
Begin with a pattern that matches too much and narrow it. Starting from an over-specific pattern and loosening it tends to produce something that fits your sample and nothing else.
-
Set the flags deliberately
g finds every match rather than the first, i ignores case, m makes ^ and $ match at line boundaries rather than only at the ends of the string, s lets a dot match newlines.
-
Read the capture groups
Parentheses create a numbered group; named groups use (?<name>...). Use a non-capturing group (?:...) when you only need to apply a quantifier, so your group numbering stays stable.
-
Prefer lazy quantifiers when scanning markup
A greedy .* runs to the last possible match on the line. The lazy .*? stops at the first, which is nearly always what you want between two delimiters.
-
Test the negative cases
Add strings that must not match and confirm they do not. This is the step that catches the over-permissive pattern that would otherwise reach production.
Example
A lazy quantifier stops at the first closing bracket; a greedy one would run to the last.
Pattern and text
Pattern: \[(.*?)\]
Flags: g
Text: [alpha] and [beta]
Matches
Match 1: [alpha] group 1: alpha
Match 2: [beta] group 1: beta
Frequently asked questions
Which regex flavour does this use?
What is catastrophic backtracking?
Why does my pattern only find the first match?
Should I parse HTML with a regex?
Is my pattern or text sent anywhere?
Tools used in this guide
All tools →- Regex Tester Test a regular expression and see every match highlighted. In your browser
- Find and Replace Replace text everywhere, with optional regex support. In your browser
- Email Extractor Pull every email address out of a block of text. In your browser
- URL Extractor Find every link in text or HTML. In your browser
- Text Difference Checker Compare two versions of a text and see what changed. In your browser
Related guides
All guides →Last reviewed .