You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求能匹配含未知值(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 uu but day/year are valid (like uu/12/2018),
  • Both month and day are uu but year is valid (like uu/uu/2018),
  • All parts are uu (like uu/uu/uu)

But it should reject dates where:

  • Day is uu but month is valid (e.g., 12/uu/2018),
  • Day and year are uu but month is valid (e.g., 12/uu/uu),
  • Year is uu but 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 date uu/uu/uu
  • ^(?:uu/uu/(?:(?:1[6-9]|[2-9]\d)?\d{2}))$: Matches dates where month and day are uu, 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 is uu, 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 like 12/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., 23 for 2023) and 4-digit years starting from 1600, which aligns with the original regex's behavior.

内容的提问来源于stack exchange,提问作者aggicd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:50:37