When to use it

Use this prompt when you need a regular expression for validation, search, log parsing or data cleaning and do not want to spend half an hour on a regex tester. It also helps when you have an existing regex that nobody understands: paste it in place of the task and ask for the explanation and the test table.

How to customize it

  • [REGEX ENGINE] matters more than most people expect. Lookbehind, named groups, Unicode classes and flags differ between JavaScript, Python, Java, .NET, POSIX tools and databases.
  • [TASK] should describe the real format, not just a name. “UK postcode” can mean a strict official format or a loose check for user input.
  • [USAGE] decides whether you need anchors (^...$) for full validation or a search pattern with capture groups.
  • [EXAMPLES] are the most valuable input. Include real strings from your data, especially the awkward ones.
  • [LANGUAGE] for the test snippet should match the code where the regex will live.

Why it works

Regular expressions are a classic case where an answer can look right and still be wrong. Asking the model to restate the rules first exposes misunderstandings before you test anything. The token-by-token explanation makes the pattern reviewable by someone who is not a regex expert.

The test table and runnable snippet are the key part: they turn the model’s claim into something you can check in seconds. Models do make mistakes with quantifiers and escaping, so running the tests yourself is not optional. The backtracking warning matters for patterns applied to user input, where a slow regex can become a denial-of-service risk.

Tips and variations

  • Paste any failing test output back and ask for a corrected regex.
  • For emails, URLs and dates, ask whether a parsing library would be safer than a regex; often it is.
  • Add “Prefer readability over brevity” for patterns other people will maintain.
  • Keep the generated test cases in your test suite so future edits do not break the pattern.
  • See our prompting patterns cheat sheet for why examples improve results.