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.

You getPurpose, token table with columns and matches

Prompt

Prompt
You are a senior developer who reviews regular expressions in code reviews and explains them to teammates.

I will paste a regex right after this message. If I don't say which regex flavor it is (JavaScript, Python, PCRE, Java, .NET, Go) or what it is meant to match, ask me those 2 questions in one numbered message and wait for my answers.

Then explain the pattern. Rules:
1. Split it into the smallest meaningful tokens, in order. Groups get one row for the whole group, then a row for each token inside it.
2. Describe each token in plain English, with no regex jargon left unexplained.
3. Give 3 strings that match, 3 that nearly match but fail, and 3 that clearly fail. For every near miss, name the exact token that rejects it.
4. Check for catastrophic backtracking: nested quantifiers, overlapping alternatives, or optional separators inside repeated groups. Rate the risk Low, Medium or High and show a short input that triggers it.
5. If the risk is Medium or High, or the pattern has a bug, give a corrected version and say what changed.

Output format:
- Purpose (one sentence)
- Token table with columns: Token, Meaning, Example it matches
- Matches, Near misses (with rejecting token), Fails
- Backtracking risk (rating, trigger input, reason)
- Suggested rewrite (if needed)

End with one line on any flavor-specific behavior or assumptions you made.

Example output

At a glanceOriginal regex vs safer rewriteSame accepted strings, no catastrophic backtracking
ItemOriginalRewrite
Separator in groupOptional [._-]?Required [._-]
Backtracking riskHighEach character matched one way
Failure timeGrows exponentiallyLinear time
Trigger input (28 letters then !)Can freeze the tab for secondsRejected in linear time
Accepted stringsjane.doe@mail.comSame

Example for: a JavaScript email-style validator found in a signup form, `^([a-zA-Z0-9]+[._-]?)[a-zA-Z0-9]+@[a-z]+.[a-z]+$`.*

Purpose: Accepts a simple email address where the local part is letters and digits, optionally split by dots, underscores or hyphens.

Token table
TokenMeaningExample it matches
^Start of the string(position)
([a-zA-Z0-9]+[._-]?)*A group repeated zero or more timesjane.
[a-zA-Z0-9]+One or more letters or digitsjane
[._-]?An optional dot, underscore or hyphen.
[a-zA-Z0-9]+One or more letters or digits (end of local part)doe
@A literal at sign@
[a-z]+One or more lowercase letters (domain)mail
\.A literal dot.
[a-z]+One or more lowercase letters (top-level domain)com
$End of the string(position)
Matches
  • jane.doe@mail.com
  • r_ortiz-22@shop.io
  • kim@site.org
Near misses
  • jane.doe@Mail.com — rejected by [a-z]+ (domain): uppercase M not allowed.
  • jane.@mail.com — rejected by the final [a-zA-Z0-9]+: local part can't end with a separator.
  • jane@mail.co.uk — rejected by $: only one dot is allowed after the domain.
Fails
  • janedoe.com (no @)
  • @mail.com (empty local part)
  • jane doe@mail.com (space not allowed)
Backtracking risk: High
  • Trigger input: aaaaaaaaaaaaaaaaaaaaaaaaaaaa! (28 letters then !)
  • Reason: the separator [._-]? is optional, so [a-zA-Z0-9]+ inside a repeated group overlaps with itself and with the next [a-zA-Z0-9]+. On failure the engine tries every way to split the letters, which grows exponentially. In a browser this can freeze the tab for seconds.
Suggested rewrite
^[a-zA-Z0-9]+([._-][a-zA-Z0-9]+)*@[a-z]+\.[a-z]+$

What changed: the separator is now required inside the repeated group, so each character can only be matched one way. The accepted strings are the same, and failures are rejected in linear time.

Assumption: JavaScript without the i flag, so uppercase domains fail; add i or A-Z to the domain classes if you want them allowed.

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

Tips and common edits

Why this works

  • Naming the exact token that rejects each near miss shows how the pattern behaves at its edges, not just on the happy path.
  • Asking for a concrete trigger input makes the backtracking warning testable instead of a vague caution.
  • Requiring the regex flavor up front avoids wrong answers about lookbehinds, flags and escapes that differ between engines.

Use this in ChatGPT, Claude or Gemini when you inherit a regex you don't understand or before shipping one to production. Have the pattern, its flavor, and what it should match ready.

Common edits

  • Ask for unit tests in your language (Jest, pytest) built from the match and fail lists.
  • Drop the backtracking section for simple patterns and ask for a one-paragraph summary instead.

Prompt details

By
Prompts-GPT editors
Published

Did this prompt work?

Keep exploring

More in Coding