During a schema-on-read validation step in your ETL pipeline, you must reject any record whose order_date field is not a valid calendar date in the form YYYY-MM-DD. The rule should allow only years between 1900 and 2099, months 01-12, and days 01-31; it does not need to account for month-specific day limits (for example, 31 February may pass). Which regular expression best enforces this requirement?
The goal is to keep the pattern tight enough to eliminate obviously invalid tokens but avoid excessive complexity. Anchoring the pattern with ^ and $ ensures that the entire string is validated, not just a substring.
The expression ^(19|20)\{2}-(0[1-9]|1[0-2])-(0[1-9]|\d|3)$ works as follows:
(19|20)\\d{2} constrains the year to 1900-2099.
(0[1-9]|1[0-2]) forces the month to 01-12.
(0[1-9]|\\d|3) correctly limits the day to 01-31 by handling numbers from 01-09, 10-29, and 30-31.
Each part is separated by the required hyphen.
Distractor explanations:
^(19|20)\\d{2}/... uses slashes, so it fails the hyphen requirement.
^\\d{4}-\\d{2}-\\d{2}$ allows 0000-00-00 and other impossible values because it lacks specific range checks.
^([0-9]{2}){2}-... repeats a two-digit group for the year (e.g., a year like 9919 would pass) and provides an incomplete day range, so many invalid years and days would pass.
Therefore, the first option is the most precise fit for the stated constraint.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What does the ^ and $ in a regular expression do?
Open an interactive chat with Bash
Why does (19|20)\d{2} constrain the year to 1900-2099?
Open an interactive chat with Bash
How does (0[1-9]|1[0-2]) ensure the month is valid?