---
name: regex-pattern-generator
description: "A tight regex for a custom text pattern you describe, honouring allowed characters, length bounds, case rules, and flags, returned as JSON so it can be dropped straight into config or code. Use when a custom text format such as a username, SKU, or ticket code needs a regex with exact allowed characters and length limits, returned as JSON. Do not use for standard formats like email or URL that have a known pattern, or explaining a pattern you already have. Related topics: coding, regex."
---

# RegEx Pattern Generator

A tight regex for a custom text pattern you describe, honouring allowed characters, length bounds, case rules, and flags, returned as JSON so it can be dropped straight into config or code.

## When to use

Use this skill when a custom text format such as a username, SKU, or ticket code needs a regex with exact allowed characters and length limits, returned as JSON. Do not use it for standard formats like email or URL that have a known pattern, or explaining a pattern you already have.

## 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. Translate the allowed characters into one explicit character class and the length bounds into a single quantifier on it.
3. Anchor the pattern unless the description calls for finding matches inside larger text, and settle the case rule through the i flag.
4. Keep only the flags that change behaviour for this use, dropping g and y for validation patterns.
5. Test the pattern against a valid string, an out-of-range length, and a disallowed character, and adjust until all three behave.
6. Write the explanation so each part of the pattern maps to one sentence.

## Inputs

- **Pattern description** (longer free text)
- **Allowed characters** (free text, e.g. "letters and digits plus @#$")
- **Minimum length** (number from 0 to 100)
- **Maximum length** (number from 1 to 200)
- **Case** (choose one: case-sensitive, case-insensitive)
- **Flags** (choose any: g, i, m, s, u, y)

## Output

A JSON object with a regex key holding the bare pattern string and an explanation key holding a short markdown paragraph that covers each part and the flags.

## Prompt template

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

### Regex from a Pattern Description

Write a regex to this specification.

- Pattern description: {{Pattern description}}
- Allowed characters: {{Allowed characters}}
- Minimum length: {{Minimum length}}
- Maximum length: {{Maximum length}}
- Case: {{Case}}
- Flags: {{Flags}}

Build the pattern from the allowed characters as an explicit class, with the length bounds as a single quantifier on that class rather than lookaheads, so the regex stays linear. Anchor it at both ends unless the description says it should find matches inside larger text. When case-insensitive is chosen, prefer the i flag over doubled character ranges. Include only the flags that change behaviour for this pattern; drop g and y when the pattern is used for validation, since they carry lastIndex state between calls. Keep the pattern as short as the specification allows and avoid capture groups that nothing will read. Before answering, check the pattern against three strings: one that should match, one too short or too long, and one with a disallowed character.

Return a JSON object with two keys: regex, holding the pattern as a plain string without delimiters, and explanation, holding a short markdown paragraph that walks through each part and names the flags to apply.

## Pitfalls

- Enforcing length with a lookahead such as (?=.{3,20}$) when a quantifier on the class does it in linear time.
- Returning the pattern wrapped in slashes, which breaks when pasted into a constructor or config file.
- Setting the g flag on a validation regex, so test() alternates true and false on repeated calls.
