Write a regex solution for this exact validation or extraction task. Environment and regex engine: [LANGUAGE AND ENGINE] What valid input looks like: [VALID INPUT RULES] Examples that must match: [VALID EXAMPLES] Examples that must not match: [INVALID EXAMPLES] How I will use it: [USAGE CONTEXT] First, restate the acceptance rule in plain English and call out any rule that is ambiguous. Then provide: 1. The regex in a code block, using delimiters and flags appropriate to [LANGUAGE AND ENGINE]. 2. A line-by-line explanation of every group, anchor, quantifier, escape, and flag. 3. A minimal code example showing the correct API for [USAGE CONTEXT], including whether it performs a full-string match, search, replace, or repeated extraction. 4. A compact test table with each supplied example, expected result, and actual result. 5. Edge cases I should decide explicitly, such as Unicode versus ASCII, line breaks, optional whitespace, case sensitivity, duplicate separators, and catastrophic backtracking risk. Prefer readable constructs and named groups when the engine supports them. Anchor with ^ and $ or use the engine's full-match API when validating an entire value; do not confuse searching with validation. Avoid lookbehind unless [LANGUAGE AND ENGINE] supports it. Do not propose regex for a nested or recursively structured format if a parser is safer; explain the alternative. Before answering, self-check that every valid example matches, every invalid example fails, and that the pattern does not unintentionally accept a substring of an invalid full value. Ask up to 3 clarifying questions only if a required input is missing.
Fill in
| Placeholder | What to enter | Example |
|---|---|---|
| [LANGUAGE AND ENGINE] | Name the programming language, library, or tool that will run the regex, such as JavaScript or Python re. | JavaScript using the built-in RegExp API |
| [VALID INPUT RULES] | Describe the permitted structure, characters, lengths, and optional parts. | US invoice IDs are INV- followed by exactly six digits; uppercase prefix only and no spaces. |
| [VALID EXAMPLES] | Provide several representative strings that must be accepted or extracted. | INV-000143, INV-827401 |
| [INVALID EXAMPLES] | Provide close-but-invalid strings that must be rejected. | inv-000143, INV-12345, INV-1234567, Invoice INV-000143, INV-12A143 |
| [USAGE CONTEXT] | State whether you are validating a field, extracting text, replacing content, or another operation. | Validate the entire value submitted in a React form field. |
How to use
- Replace the placeholders with rules and examples from the system that owns the data.
- Include negative examples that differ by one character, one separator, or one extra word.
- Run the supplied code in your actual runtime, especially if it uses lookbehind, Unicode properties, or multiline input.
- Follow up with: “Add five adversarial test cases for this pattern and explain any performance risk on a 10,000-character string.”
Variations
Extract fields
Use this to pull structured parts from unstructured text.
Create an extraction regex for [LANGUAGE AND ENGINE]. From text like [SAMPLE TEXT], capture [FIELDS TO CAPTURE] while ignoring [EXCLUSIONS]. Show the pattern with named capture groups if supported, then provide code that returns every match and maps each group to a field. Include examples that contain multiple matches and near-matches. Check that the pattern does not consume text across an unintended line or sentence boundary, and state whether global or multiline flags are needed.
Replacement rule
Use this to normalize or redact text safely.
Write a safe regex replacement for [LANGUAGE AND ENGINE]. Input format: [INPUT FORMAT]. I need to transform it into [DESIRED OUTPUT], while preserving [PARTS TO PRESERVE]. Provide the regex, replacement string or callback, and runnable code. Include a before-and-after table for [EXAMPLES]. Explain whether the replacement is global and how capture groups are referenced in this engine. Check that malformed inputs are left unchanged or handled as I specify; do not silently delete unrelated text.
Log parsing
Use this to extract fields from application logs.
Design a regex for parsing these [LOG TYPE] lines in [LANGUAGE AND ENGINE]: [LOG SAMPLES]. Extract [FIELDS], allowing [OPTIONAL PARTS] but rejecting [MALFORMED CASES]. Return a pattern, a small parsing function, and a test table. Account for timestamps, quoted values, escaped characters, and greedy matching where relevant. If the log format is structured JSON or CSV, explain why a real parser is safer and show that approach instead.
Tips
- Write examples before the regex: three accepted values and at least three nearly valid rejected values expose most missing rules.
- For field validation, use a full-match API or anchors; a search pattern can find a valid fragment inside an invalid value.
- Keep regexes for flat patterns such as IDs and dates; parse JSON, HTML, nested parentheses, and CSV with proper parsers.
- Use the exact target engine because escapes, Unicode support, named groups, and lookbehind differ across JavaScript, Python, PCRE, and RE2.
FAQ
Can ChatGPT generate a regex from one example?
It can, but one example does not define the rule. Give examples that must fail as well as examples that must pass.
Why does a regex work in an online tester but not in my code?
The tester may use a different engine or flags. Specify the language and whether your code uses match, search, test, find, or replace.
How do I know a regex is safe?
Ask for adversarial test cases and backtracking risk, particularly when nested optional groups or repeated alternatives are involved. Put length limits on untrusted input where practical.