求能匹配含未知值(uu)特定日期格式的正则表达式
Solution for Target Date Matching with
uu Placeholders Alright, let's break down what you need here: you want a regex that matches dates where either:
- It's a fully valid calendar date (like
12/12/2018), - The month is
uubut day/year are valid (likeuu/12/2018), - Both month and day are
uubut year is valid (likeuu/uu/2018), - All parts are
uu(likeuu/uu/uu)
But it should reject dates where:
- Day is
uubut month is valid (e.g.,12/uu/2018), - Day and year are
uubut month is valid (e.g.,12/uu/uu), - Year is
uubut month/day are valid (e.g.,12/12/uu)
Here's the adjusted regex based on Ofir Luzon's original valid date pattern, modified to include the uu placeholder rules:
^(?:uu/uu/uu)$|^(?:uu/uu/(?:(?:1[6-9]|[2-9]\d)?\d{2}))$|^(?:uu/(?:(?:31\/(?:0?[13578]|1[02]))|(?:(?:29|30)\/(?:0?[13-9]|1[0-2]))|(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2])))(?:(?:1[6-9]|[2-9]\d)?\d{2}))$|^(?:(?:31\/(?:0?[13578]|1[02]))|(?:(?:29|30)\/(?:0?[13-9]|1[0-2]))|(?:29\/0?2\/(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)?\d{2}))$
Let's break down each section of this regex:
^(?:uu/uu/uu)$: Matches the fully placeholder dateuu/uu/uu^(?:uu/uu/(?:(?:1[6-9]|[2-9]\d)?\d{2}))$: Matches dates where month and day areuu, and the year is a valid 4-digit (or 2-digit for modern eras) year^(?:uu/(?:(?:31\/(?:0?[13578]|1[02]))|(?:(?:29|30)\/(?:0?[13-9]|1[0-2]))|(?:(?:0?[1-9]|1\d|2[0-8])\/(?:0?[1-9]|1[0-2])))(?:(?:1[6-9]|[2-9]\d)?\d{2}))$: Matches dates where month isuu, and the day/year form a valid calendar date (includes checks for 31-day months, 30-day months, and valid day ranges)- The final segment is the adjusted version of Ofir Luzon's original valid date regex (limited to
/as the separator), which matches fully valid calendar dates like12/12/2018
Notes:
- This regex uses
/as the date separator (matching your examples). If you need to support-or.as well, you can replace each\/with(?:\/|-|\.)(like in the original regex), but your examples all use/so this is focused on that. - The year validation supports 2-digit years (e.g.,
23for 2023) and 4-digit years starting from 1600, which aligns with the original regex's behavior.
内容的提问来源于stack exchange,提问作者aggicd
相关产品推荐
相关产品推荐

