---
name: regex-pattern-formatter
description: "Turns an unreadable one-line regex into a commented, line-by-line version in your language's verbose or concatenated form, so reviewers can follow each part and the pattern still runs unchanged. Use when an existing regex is too dense to review and needs splitting into commented lines without changing what it matches. Do not use for writing a new regex from a description or making a slow regex faster. Related topics: coding, regex."
---

# RegEx Pattern Formatter

Turns an unreadable one-line regex into a commented, line-by-line version in your language's verbose or concatenated form, so reviewers can follow each part and the pattern still runs unchanged.

## When to use

Use this skill when an existing regex is too dense to review and needs splitting into commented lines without changing what it matches. Do not use it for writing a new regex from a description or making a slow regex faster.

## 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. Read the raw pattern and state in one sentence what it actually matches, noting any gap from the stated purpose.
3. Split the pattern at anchors, groups, alternations, and quantified classes, one logical piece per line under the length limit.
4. Use the flavor's verbose mode where it exists, or commented string concatenation for JavaScript, escaping literal spaces and hashes in verbose mode.
5. Comment each line with what it matches in plain words, not a restatement of the regex syntax.
6. When tests are requested, add a table of example strings with match or no match, and confirm the formatted pattern gives the same results as the original.

## Inputs

- **Raw pattern** (longer free text)
- **What it validates** (free text, e.g. "brief description of what the pattern matches")
- **Language or flavor** (choose one: JavaScript, Python, PCRE, Java, .NET)
- **Comment style** (choose one: // line comment, # line comment, /* block comment */)
- **Max line length** (number from 40 to 120)
- **Include example tests** (choose one: Yes, No)

## Output

One code block in the chosen language holding the formatted and commented pattern, an optional table of test strings with match results, and a one-line note on any suspected defect.

## Prompt template

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

### Commented Regex

Format and comment this regex.

- Raw pattern: {{Raw pattern}}
- What it validates: {{What it validates}}
- Language or flavor: {{Language or flavor}}
- Comment style: {{Comment style}}
- Max line length: {{Max line length}}
- Include example tests: {{Include example tests}}

First state what the pattern matches in one sentence, from reading it, and flag any gap between that and the stated purpose. Then split the pattern at its natural boundaries: anchors, groups, alternations, quantified classes. Use the flavor's own verbose mode where it exists (re.X in Python, (?x) in PCRE, .NET, and Java) so the comments live inside the pattern; for JavaScript, which has no verbose mode, build the pattern from commented string pieces joined with concatenation or String.raw. Keep every line under the limit. Preserve the pattern's behaviour exactly: in verbose mode, escape literal spaces and hashes, since they otherwise become whitespace and comments. When tests are requested, add a short table of example strings with match or no match.

Return one code block in the chosen language with the formatted, commented pattern, then, if requested, the test table. Close with a one-line note on any spot where the original pattern looked wrong for its stated purpose.

## Pitfalls

- Enabling verbose mode without escaping a literal space or hash, which silently changes what the pattern matches.
- Commenting with the regex syntax restated ('one or more digits') instead of the meaning ('the area code').
- Splitting inside a character class or between a token and its quantifier, which breaks the pattern.
