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

Java中Cron表达式正则解析的特定单词精确匹配问题求助

Fixing Your Cron Regex to Match Exact Month/Weekday Abbreviations

Got it, let's fix that regex issue for you! The problem with your current pattern is two-fold:

  1. You wrapped your month/weekday options inside a character class ([]), which treats them as individual characters instead of whole, distinct terms.
  2. There was no enforcement that the abbreviations are matched as complete, exact values—so strings like JAAAN slipped through because they contained valid characters from JAN.

Key Fixes Explained

  • Replace the broken character class wrapping for months/weekdays with non-capturing groups that explicitly match full, valid abbreviations.
  • Define valid individual terms (like JAN, MON, ranges, wildcards, intervals) and ensure they can only be separated by commas, with no extra characters allowed.

Final Corrected Regex

^([\d,\-\*\/]*\s+){3}([(LW)\d,\-\*\?\/]+\s+)(?:\*|\?|L|\d{1,2}|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)|(?:(?:\d{1,2}|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-(?:\d{1,2}|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))|(?:(?:\*|\d{1,2}|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\/\d+)|(?:(?:\d{1,2}|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)#\d+)(?:,(?:\*|\?|L|\d{1,2}|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)|(?:(?:\d{1,2}|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-(?:\d{1,2}|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))|(?:(?:\*|\d{1,2}|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)\/\d+)|(?:(?:\d{1,2}|JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)#\d+))*\s+)(?:\*|\?|L|\d{1,2}|(?:SUN|MON|TUE|WED|THU|FRI|SAT)|(?:(?:\d{1,2}|SUN|MON|TUE|WED|THU|FRI|SAT)-(?:\d{1,2}|SUN|MON|TUE|WED|THU|FRI|SAT))|(?:(?:\*|\d{1,2}|SUN|MON|TUE|WED|THU|FRI|SAT)\/\d+)|(?:(?:\d{1,2}|SUN|MON|TUE|WED|THU|FRI|SAT)#\d+)(?:,(?:\*|\?|L|\d{1,2}|(?:SUN|MON|TUE|WED|THU|FRI|SAT)|(?:(?:\d{1,2}|SUN|MON|TUE|WED|THU|FRI|SAT)-(?:\d{1,2}|SUN|MON|TUE|WED|THU|FRI|SAT))|(?:(?:\*|\d{1,2}|SUN|MON|TUE|WED|THU|FRI|SAT)\/\d+)|(?:(?:\d{1,2}|SUN|MON|TUE|WED|THU|FRI|SAT)#\d+))*\s*)([\d,\-\*\/\#]+){0,1}$

What This Regex Supports

Your original Cron features are all preserved, plus strict validation:

  • Valid month abbreviations: JAN, FEB, ..., DEC (no extra characters allowed)
  • Valid weekday abbreviations: SUN, MON, ..., SAT (same strictness)
  • Wildcards (*), optional values (?), ranges (JAN-MAR, MON-FRI), intervals (*/5, JAN/2), and special characters like L/# (if your Cron implementation uses them)
  • Valid numeric values for months/weekdays

Test Cases

  • ✅ Matches your original valid Cron: 0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010
  • ❌ Rejects invalid months: 0/5 14,18,3-39,52 * ? JAAAN,MAR,SEP MON-FRI 2002-2010
  • ❌ Rejects invalid weekdays: 0/5 14,18,3-39,52 * ? JAN,MAR,SEP MONN-FRI 2002-2010

Optional Simplification

If your Cron implementation doesn't support L or # for months/weekdays, you can remove those sections from the regex to make it shorter and more focused on your actual use case.

内容的提问来源于stack exchange,提问作者Nikita.Itransition

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 22:37:45