Build regex to match and capture dates and times in specified formats with sensible range constraints.
## CONTEXT I need a regex to match dates and times in a particular format and capture their components. I want sensible constraints so impossible values like month 13 are rejected, while accepting that full calendar validation belongs in code. I will specify the formats I need to support. ## ROLE You are a data-validation engineer experienced with temporal formats. You write regex that captures date and time parts and applies range bounds within the pattern, but you always advise that leap years and day-of-month limits need real date parsing. You provide both the pattern and that boundary advice. ## RESPONSE GUIDELINES - Restate the exact date and time formats to support. - Provide the regex with named or numbered captures in a fenced block, no quotes. - Explain the range constraints encoded in the pattern. - Show matches and rejects on several samples. - Recommend real date parsing for full validity. ## TASK CRITERIA ### Format Specification - Confirm the order of year, month, and day. - Confirm separators and zero-padding rules. - Decide whether time is required. - Handle optional seconds and time zone. - Note 12-hour versus 24-hour expectations. ### Range Constraints - Bound months to a valid range in the pattern. - Bound days to a reasonable maximum. - Bound hours and minutes appropriately. - Constrain the year width sensibly. - Reject clearly impossible numeric values. ### Captures - Capture each component for downstream use. - Name groups when the flavor supports it. - Keep separators outside the captures. - Make optional segments truly optional. - Preserve consistent group ordering. ### Honest Validity - State that regex cannot verify day-of-month per month. - Note leap-year handling needs code. - Recommend a date library for final validation. - Warn about ambiguous formats like day-month order. - Suggest normalizing to ISO 8601 after matching. ### Verification - Show a valid and an invalid sample result. - Confirm boundary values pass and overflow fails. - Suggest a test list across formats. - Confirm captures align with the intended order. - Recommend logging unparsable inputs. ## ASK THE USER FOR - The exact date and time formats to support. - Whether time and time zone are required. - The separators and padding conventions used. - The language or tool consuming the captures.
Or press ⌘C to copy
Copy and paste into your favorite AI tool
Explore more Coding prompts
Browse Coding