5 ChatGPT Prompts for Regex Patterns

Writing a regex is not the hard part. The hard part is coming back to it three weeks later and being afraid to touch it. These five ChatGPT prompts move the work from “make it match” to “make it explainable”: examples first, pattern second, and a review pass before it ships.

What to know before you paste

  • Say which language and flavor you are targeting. Lookbehind support and named-group syntax differ, and a pattern that works in one engine can fail in another.
  • Give real strings, not descriptions of strings. Two matching examples and two non-matching ones beat a paragraph of requirements.
  • Paste production code with secrets and real user data removed. Placeholders are enough for the pattern to be tested.

The prompts

1. Pattern from matching examples

These strings should match: [matches]. These should not: [non-matches]. Write the regex for [language] and explain each group in one line.

What it does: Builds the pattern from evidence instead of a description.

How to use: replace both lists and [language]; run the pattern against your whole sample before trusting it.

2. Explain one you inherited

Explain this regex piece by piece in plain English: [regex]. Then list two inputs it would match that the author probably did not intend.

What it does: Turns a line of punctuation into something you can defend in review.

How to use: replace [regex]; test the two unintended matches before you decide whether they matter.

3. Named groups and verbose mode

Rewrite this regex using named groups and a verbose flag so it is readable: [regex]. Keep the behavior identical.

What it does: Makes the pattern self-documenting without changing what it does.

How to use: replace [regex]; diff the old and new results on the same input before committing.

4. Test cases before the pattern

For this requirement: [requirement], write ten strings that must match and ten that must not. Then write the regex that satisfies all of them.

What it does: Forces the edge cases to exist before the code does.

How to use: replace [requirement]; keep the twenty strings as a test, not a scratch file.

5. Review for the dangerous parts

Review this regex for catastrophic backtracking and edge cases: [regex]. Point to the specific construct that worries you and give a safer form of it.

What it does: Catches the pattern that works on short input and hangs on long input.

How to use: replace [regex]; test the safer version with a long, adversarial string, not a friendly one.

FAQ

Should I use regex for HTML?

Rarely. HTML is not a regular language, and patterns that seem to work break on nesting and attributes in unexpected order. Use a parser unless you are matching a single, well-bounded fragment like one attribute value.

Why does my regex work in one language but not another?

Different engines support different features. Lookbehind, possessive quantifiers and named-group syntax vary, and some engines treat unicode classes differently by default. Always name the target language when you generate the pattern.

How do I match across multiple lines?

You need two separate ideas: a flag that lets the dot match newlines, and anchors that treat each line as its own start and end. Setting one and expecting both behaviours is the usual mistake.

Is a long regex always wrong?

Not always, but length without names is. If a long pattern uses named groups and comments, it can be maintainable. If it is one unreadable line, split the job into two simpler matches and combine the results in code.

How do I debug a pattern that matches too much?

Print what each group captured on a failing input, not just whether the whole thing matched. The overreach is usually one greedy quantifier or a character class that is wider than you think.

Keep going

Every prompt on GuPrompt is free to copy — no signup, no paywall.

Leave a Comment