Build a regular expression for a stated purpose and regex flavour, explain each part, and test it against matching and non-matching examples, edge cases and backtracking risk.
Create a regular expression for the purpose below in the stated regex flavour. Explain it, test it against the examples, and say what it intentionally does not validate. ## Inputs Purpose: what to match, and whether the pattern must match the whole string or find occurrences inside text: [WHAT_TO_MATCH] Regex flavour and where it runs (JavaScript, Python re, PCRE, Java, Go RE2, PostgreSQL): [FLAVOUR] Examples that should match: [EXAMPLES] Examples that should NOT match: [COUNTER_EXAMPLES] Input source and size (form field, log lines, large files, untrusted user input): [INPUT_CONTEXT] ## Instructions 1. Use anchors for whole-string validation, or word boundaries/lookarounds for extraction, according to [WHAT_TO_MATCH]. 2. Use only syntax supported by [FLAVOUR]; mention flags needed (case-insensitive, multiline, Unicode). 3. Prefer specific character classes over `.*`; avoid nested quantifiers that can cause catastrophic backtracking on untrusted input. 4. If a rule is better checked in code (for example calendar validity, checksums), say so and limit the regex to format. ## Return 1. The regex pattern, with flags, in a code block. 2. Explanation of each part. 3. Edge cases handled and not handled. 4. Test cases: a table of every provided example plus added edge cases, with expected match or no match. 5. Performance considerations for [INPUT_CONTEXT]. 6. A short test snippet in the language of [FLAVOUR]. ## Acceptance checks - Every example in [EXAMPLES] matches and every example in [COUNTER_EXAMPLES] does not, as shown in the test table. - Limitations are stated, not hidden. - No nested unbounded quantifiers on untrusted input. - Expected results are labeled as expected unless the snippet was run.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Purpose: validate a whole form field containing an ISO 8601 calendar date in YYYY-MM-DD form Flavour: Python re Should match: 2025-01-31, 1999-12-01, 2024-02-29 Should not match: 2025-13-01, 2025-1-05, 2025-01-32, 20250101, 2025-01-31T10:00 Input: single form field from untrusted users
A pattern such as ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ used with re.fullmatch (or with the anchors), an explanation of each group, a limitation stating it accepts impossible dates such as 2025-02-30 so calendar validity must be checked with datetime.date.fromisoformat, a test table covering all eight examples plus 2025-02-30, and a note that the pattern has no nested quantifiers. This exact pattern was executed locally against these examples (see report); a model's answer may differ.Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
Models