---
name: regex-explainer-and-tester
description: "Explains a regular expression piece by piece, then tests it against matching and non-matching cases and fixes it where it fails or risks catastrophic backtracking. Use when a regular expression is hard to read or behaves unexpectedly and needs to be explained token by token and checked against examples. Do not use for writing a parser or extracting data from a large text corpus. Related topics: coding, productivity."
---

# Regex Explainer and Tester

Explains a regular expression piece by piece, then tests it against matching and non-matching cases and fixes it where it fails or risks catastrophic backtracking.

## When to use

Use this skill when a regular expression is hard to read or behaves unexpectedly and needs to be explained token by token and checked against examples. Do not use it for writing a parser or extracting data from a large text corpus.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Split the pattern into tokens and explain each in plain words, including greedy versus lazy behaviour and group capture.
3. Restate the whole pattern in one sentence and compare it with the stated intent.
4. Trace every supplied example, recording match or no match, the captured text and the reason.
5. Add edge cases for empty input, whitespace, Unicode, newlines and long strings, and predict each result.
6. Check for catastrophic backtracking and give a corrected pattern with before and after results if anything fails.

## Inputs

- **Regex** (free text, e.g. "Paste the pattern")
- **Regex flavour** (choose one: JavaScript, Python, PCRE, Java, Go, .NET)
- **What it should match** (longer free text)
- **Strings that should match** (longer free text)
- **Strings that should not match** (longer free text)
- **Flags used** (free text, e.g. "i or g or m or none")

## Output

A token-by-token explanation, a one-sentence summary, a results table of examples and edge cases, and a corrected pattern with a change list when needed.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Regex Explainer and Tester

Explain and test this regular expression.

- Regex: {{Regex}}
- Regex flavour: {{Regex flavour}}
- What it should match: {{What it should match}}
- Strings that should match: {{Strings that should match}}
- Strings that should not match: {{Strings that should not match}}
- Flags used: {{Flags used}}

Break the pattern into tokens left to right and explain each in plain words: anchors, groups and whether they capture, character classes, quantifiers and whether they are greedy or lazy, alternations and lookarounds. Restate the whole pattern as one sentence and compare it with the stated intent, noting where they differ. Trace the pattern against every example given, saying for each whether it matches, which part of the string is captured, and why, being strict about the anchors and the flags. Add edge cases the author did not list, such as empty input, extra whitespace, Unicode, very long strings and newlines, and predict the result. Check for backtracking danger such as nested quantifiers on overlapping classes. If the pattern is wrong, give a corrected one with a note of each change and the table of test results before and after. Avoid pretending to run code you did not run: say that results are traced by hand.

## Pitfalls

- Forgetting that anchors and the multiline flag change what a match means, so a pattern passes by accident.
- Assuming the same syntax across flavours, when lookbehind and named groups differ between engines.
- Claiming a pattern is safe without checking nested quantifiers that can freeze a server on hostile input.
