Testing a regular expression properly
A regular expression that matches your one example is not finished; it is only started. The reliable way to write a regex is to collect strings that must match and strings that must not, then grow the pattern until it sorts them correctly. This page walks through that process with a date pattern.
Try a pattern (JavaScript flavour)
The tester lists every match with its position and capture groups, and updates as you type. It always searches globally. Very slow patterns can freeze the tab, so read the backtracking section before testing nested quantifiers on long text.
The method: two lists first, pattern second
- Write a “must match” list. Include the ordinary case and the edge cases: the smallest and largest values, leading zeros, the end of a month.
- Write a “must not match” list. These are near misses: the wrong order, a missing zero, an out-of-range number, extra text around a valid value.
- Start with the simplest pattern that matches the first list, then tighten it one piece at a time.
- After every change, re-run both lists. A fix for one case often breaks another.
- Keep the lists as unit tests next to the code, so the next person can change the pattern safely.
Worked example: an ISO date
Goal: accept YYYY-MM-DD. A first attempt, \d{4}-\d{2}-\d{2}, accepts 2030-13-01, which has no month 13. Tightening each part:
| Part | Pattern | Why |
|---|---|---|
| Year | \d{4} | Exactly four digits |
| Month | 0[1-9]|1[0-2] | 01–09 or 10–12 |
| Day | 0[1-9]|[12]\d|3[01] | 01–09, 10–29, or 30–31 |
| Whole line | ^…$ with the m flag | Reject extra text before or after |
With the sample strings in the box, the pattern matches 3 of 6 lines: 2030-01-15, 2030-02-30 and 1999-12-31. It rejects month 13, the day-first form and the unpadded 2030-1-5.
What the test reveals: 2030-02-30 passes, but February never has 30 days. A regex checks shape, not calendar rules. Validate the format with the regex, then confirm the date with a real date library. Knowing where a regex should stop is part of testing it.
Reading capture groups
Parentheses capture the text they match. The pattern above uses named groups, (?<year>…), so the tool reports year: "2030", month: "01", day: "15" for the first line. Named groups make code like m.groups.month readable. Use (?:…) when you need grouping for alternation or a quantifier but do not want a capture.
Greedy versus lazy: a test that surprises
Test <.+> against <b>bold</b> and <i>it</i>. You may expect four tags; you get one match covering the whole line, because .+ is greedy and grabs as much as possible before backing off to the last >. Adding ? makes it lazy: <.+?> returns <b>, </b>, <i> and </i>. An even safer form is <[^>]+>, which cannot cross a closing bracket at all.
Patterns that hang: catastrophic backtracking
A pattern such as ^(a+)+$ tested against a long run of a followed by one b makes the engine try an exponential number of ways to split the run before it gives up. Thirty characters can take seconds; a few more can lock up a page or a server. Warning signs are nested quantifiers like (x+)+ or (x*)*, and alternatives that can match the same text, such as (a|a)+. Always include a long near-miss string in your “must not match” list to catch this.
Questions
Will a pattern tested here work in Python or grep?
Mostly, but flavours differ. Python writes named groups as (?P<name>…), and basic grep needs -E for + and |. The regex cheat sheet lists the differences.
Which flags can I use?
i ignores case, m makes ^ and $ match at each line, s lets . match line breaks, and u enables full Unicode. The tool adds g itself.
Is my test text sent anywhere?
No. The browser’s own regex engine runs the test, and nothing is stored.