Debug Regex and Parsers with GitHub Copilot

Generate an automated audit of regular expressions or parser snippets that flags catastrophic backtracking, isolates edge cases, and produces unit test fixtures.

Agent prompt for ChatGPT and Claude

You get: Vulnerability and Performance Analysis, failure Root Cause and optimized Implementation

Prompt

Prompt
Act as a compiler engineer and regex security specialist. I will paste a regular expression or token parser snippet below along with sample inputs that fail or run slowly.

Analyze the code using these rules:
1. Break down the pattern into logical match groups and identify catastrophic backtracking risks (nested quantifiers or overlapping alternatives).
2. Explain why the provided sample inputs cause failures, timeouts, or unexpected captures.
3. Provide a corrected, safer pattern or equivalent sequential parser code with zero exponential complexity risk.
4. Write a compact test fixture table listing at least three valid strings, three invalid strings, and two adversarial edge cases.

Output format:
- Vulnerability and Performance Analysis (state time complexity in big-O notation)
- Failure Root Cause (bulleted breakdown of the pasted samples)
- Optimized Implementation (the clean regex or code snippet)
- Test Fixtures (Markdown table with columns: Input String, Expected Match, Purpose)

End with one short line identifying any parser engine limitations (such as ECMAScript vs PCRE2 features).

Example output

Example for: an email extraction regex failing on long strings with multiple dot segments.

Vulnerability and Performance Analysis
  • Complexity: O(2^n) worst-case time complexity on unmatched input strings.
  • Risk: The pattern uses nested greedy quantifiers: ^([a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*)+@.... The outer quantifier combined with the inner separator loop forces the regex engine to explore exponential backtrack permutations when the string lacks an @ symbol.
Failure Root Cause
  • Input user.name.test.account.with.very.long.prefix.at.domain: Causes catastrophic backtracking because the engine permutations between the primary group and the separator sub-group grow multiplicatively for every period.
  • Input dev+tag@domain.com: Fails to match because the plus symbol is omitted from the allowed character class.
Optimized Implementation
import re

# Linear O(n) regex avoiding nested quantifiers
SAFE_EMAIL_REGEX = re.compile(
    r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
)
Test Fixtures
Input StringExpected MatchPurpose
dev.tester@company.coFull matchStandard valid dotted mailbox
team+ci@app.internal.netFull matchPlus addressing support
support@sub.domain.orgFull matchMulti-level subdomains
@missinguser.comNo matchMissing user local part
user.@badend.comNo matchTrailing dot in local part
plainaddressNo matchMissing delimiter and domain
a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.pNo matchBacktracking rejection performance check
user@localhostNo matchIncomplete TLD validation

Engine limitation: Assumes standard Python re syntax without atomic groups ((?>...)) or possessive quantifiers (++).

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Forces an explicit Big-O complexity check to stop catastrophic backtracking and ReDoS vulnerabilities before deployment.
  • Requires an exact tabular test fixture with adversarial inputs to catch boundary regressions instantly.
  • Bans nested greedy loops by mandating safe, linear patterns or explicit sequential parser alternatives.

Paste this directly into GitHub Copilot Chat, Claude, or ChatGPT along with your problematic regex string or token parsing logic and sample failed inputs.

Common edits

  • Specify your language runtime (e.g. Node.js V8 or Go regexp) to swap standard regex for native non-backtracking engines.
  • Request the corrected parser as an iterative finite-state machine instead of a regular expression.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

Turn Sentry Errors into Reproducible Codex Test Cases

Transforms a production Sentry stack trace and context payload into a self-contained failing unit test designed for Codex repo tasks.

Coding

Explain Any Regex Token by Token with Matching and Failing Examples

Get a token-by-token table explaining your regex, plus strings that match, nearly match and fail, and a clear catastrophic backtracking risk check with a safer rewrite.

AI Coding AgentsClaude Code

Evaluate Coding Agent Test Suites for False Positive Traps

Audits agent-generated unit tests to catch tautological assertions, over-mocking, and tests that still pass when core business logic is disabled.

Coding

Regex Builder With Match and No-Match Test Cases

Builds a regular expression from a plain-English description and example strings, explains each part, and proves it against strings that should and should not match.

CodingCursor

Unit Test Plan and Test Code for an Existing Function

Builds a behavior-based test matrix for a function or module and then generates test code in your framework. Covers edge cases, failures, and mocking boundaries, with coverage gaps stated honestly.