Coding & Software

Write and Explain a Regular Expression

Builds a regular expression from matching and non-matching examples, explains each part and lists the edge cases it does not handle.

Designed for general-purpose AI assistantsPublished November 2026

Customize

Fill in the fields and the prompt updates. What you type stays in this page and is never saved or sent.

Preview

Write a regular expression for the task below, built from the examples I give you.

Language or tool and regex flavor: {{language or tool}}
What the pattern must do: {{task}}
Strings that must match:
{{should match}}

Strings that must NOT match:
{{should not match}}

Extra rules (anchors, case sensitivity, Unicode, multiline, maximum length): none
Is this for validation, extraction or search-and-replace? extraction

Give me:
1. The pattern, in a code block, written for {{language or tool}} including how to escape it in a string literal in that language.
2. A breakdown of the pattern, one piece per line, saying what each part matches and why it is there.
3. A table of every example I gave you with the result you expect (match or no match, and the captured groups if any). Walk through at least two of the tricky ones step by step to show you did not just assume.
4. Edge cases the pattern does not handle or handles differently from what I might expect, and a stricter or looser version if relevant.
5. Any risk of catastrophic backtracking or very slow matching on long input, and how to avoid it.
6. A short test snippet in {{language or tool}} that runs all my examples and prints the result.

Rules: only use features supported in the flavor I named. If the examples are ambiguous or conflict with each other, ask me which is right before writing the pattern. If a regular expression is the wrong tool for the task, such as parsing HTML or fully validating an email address, say so and suggest the right approach.

4 required fields are empty and will stay as a {{placeholder}} in the copied text.

How to use it

  1. Provide at least five examples that should match and five that should not, including the awkward ones you worry about. Examples define the task better than a description.
  2. Name the exact flavor. JavaScript, Python, PCRE and Go differ in lookbehind, named groups and Unicode support.
  3. Run the test snippet before trusting the pattern, and add any real-world string it gets wrong to the examples.
  4. For anything security-sensitive, such as validating input, treat the pattern as one layer and review it with a second person.

Example output

Illustrative only. Real answers depend on your input and the assistant you use.

Pattern (JavaScript):
/^(?:\+?1[ -]?)?\(?([2-9]\d{2})\)?[ -]?(\d{3})[ -]?(\d{4})$/

As a string: "^(?:\\+?1[ -]?)?\\(?([2-9]\\d{2})\\)?[ -]?(\\d{3})[ -]?(\\d{4})$"

Breakdown:
- (?:\+?1[ -]?)? optional country code 1, with an optional plus and separator
- \(?([2-9]\d{2})\)? area code (cannot start with 0 or 1), optional parentheses, captured as group 1
- [ -]? optional space or hyphen
- (\d{3}) three-digit exchange, group 2
- (\d{4}) four-digit line number, group 3

Examples: "(415) 555-0132" matches (415, 555, 0132). "415-555-0132" matches. "055-123-4567" does not match because the area code starts with 0.

Not handled: extensions such as "x123", and phone numbers from other countries.

Backtracking: the pattern has no nested quantifiers, so long input is safe.

Tips

If the assistant asks a clarifying question, answer it. Regexes fail at the boundaries, and the boundaries are in your data, not in the assistant's head.

Prefer a readable pattern with comments (use the verbose or extended flag where the language has one) over a clever one-liner you will not understand in six months.

Never use a pattern for security decisions without testing it against hostile input as well as normal examples.

  • Coding & Software

    Write Unit Tests for a Function

    Generates focused unit tests for one function, covering normal cases, boundaries and invalid input without mocking more than needed.

    Customizable fields
    3 fields
  • Coding & Software

    Explain Unfamiliar Code

    Explains code you did not write at the level you ask for, tracing the data flow and flagging what it cannot tell from the snippet alone.

    Customizable fields
    4 fields
  • Coding & Software

    Debug a Flaky Test

    Finds why a test passes sometimes and fails other times, ranks the likely causes and gives an experiment to prove each one.

    Customizable fields
    8 fields