---
name: regex-pattern-optimizer
description: "Rewrites a slow or backtracking-prone regex into an equivalent that runs in predictable time on your engine, with the changes explained and a residual risk score, in machine-readable JSON. Use when a regex times out, pins the CPU, or risks catastrophic backtracking on long inputs and needs a faster equivalent for a specific engine. Do not use for writing a new regex from a description, or commenting a pattern for readability. Related topics: coding, regex."
---

# RegEx Pattern Optimizer

Rewrites a slow or backtracking-prone regex into an equivalent that runs in predictable time on your engine, with the changes explained and a residual risk score, in machine-readable JSON.

## When to use

Use this skill when a regex times out, pins the CPU, or risks catastrophic backtracking on long inputs and needs a faster equivalent for a specific engine. Do not use it for writing a new regex from a description, or commenting a pattern for readability.

## 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. Locate every backtracking hazard in the pattern: nested quantifiers, overlapping alternations, greedy dot-star, and groups that can match empty.
3. Rewrite each hazard with the engine's tools (atomic groups, possessive quantifiers, exclusive classes, anchors) without changing the match set.
4. For Go and Rust, remove backreferences and lookarounds since the engines reject them, and note any behaviour that cannot be preserved.
5. Verify the new pattern on the sample input and on an adversarial string of the stated length, and score the residual risk from 1 to 10.
6. Package the result as the three-key JSON object with a three-sentence explanation.

## Inputs

- **Target engine** (choose one: PCRE, JavaScript, Python, Go, Rust)
- **Pattern** (longer free text)
- **Sample input** (longer free text)
- **Typical input length (characters)** (number from 1 to 1000000)
- **Primary goal** (choose any: Speed, Readability, Memory, Compatibility)
- **Allow lookarounds** (choose one: Yes, No)

## Output

A JSON object with the keys optimized_pattern, explanation (three sentences), and risk_score (integer 1 to 10), and nothing outside the JSON.

## Prompt template

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

### Regex Performance Rewrite

Optimise this regex.

- Target engine: {{Target engine}}
- Pattern: {{Pattern}}
- Sample input: {{Sample input}}
- Typical input length (characters): {{Typical input length (characters)}}
- Primary goal: {{Primary goal}}
- Allow lookarounds: {{Allow lookarounds}}

Start by finding the backtracking hazards: nested quantifiers, overlapping alternations, a greedy dot-star followed by something the dot also matches, and optional groups that can match empty. For each, rewrite with the engine's tools: possessive quantifiers or atomic groups in PCRE, mutually exclusive character classes everywhere, a negated class instead of a lazy dot, anchors so the engine does not retry at every position. Go and Rust guarantee linear time but reject backreferences and lookarounds, so compatibility means removing those rather than tuning them. Keep the match set identical: say so explicitly if any rewrite changes what matches at the edges, and prefer a slightly slower pattern over a different one. Score the remaining catastrophic-backtracking risk from 1 (linear) to 10 (exponential on plausible input) based on the input length given.

Return a JSON object with exactly three keys: optimized_pattern (the new regex as a plain string), explanation (three sentences on what changed and why it is faster), and risk_score (an integer from 1 to 10). No markdown or commentary outside the JSON.

## Pitfalls

- Replacing a greedy dot-star with a lazy one, which still backtracks on failure and only moves the cost around.
- Quietly dropping a lookaround for Go compatibility so the pattern now matches strings the original rejected.
- Scoring risk from the sample input alone when the hazard only appears on long inputs that nearly match.
