使用特定列出的单词解析 Java 中的 cron 表达式的问题

问题描述

我有一个带有符号单词和数字的 cron 表达式:

0/5 14,18,3-39,52 * ? JAN,MAR,SEP MON-FRI 2002-2010

而且我有一个用于解析 Java 的 cron 表达式的正则表达式:

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

它几乎可以正常工作,但是有一个问题。如果我为任何单词添加一个额外的字母(例如:'JAAAN'),它也会解析这个 cron 表达式。 我需要帮助。我如何解决它以仅解析所有组合中的特定单词(如所列),例如“JAN”或“FEB”,但不解析“JANU”或“FEBR”。

拜托,我需要帮助!

解决方法

你没有提供足够的数据,所以它不会很漂亮:

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

regex proof

说明

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1 (3 times):
--------------------------------------------------------------------------------
    [\d,*\/-]*               any character of: digits (0-9),','*','\/','-' (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    \s+                      whitespace (\n,\r,\t,\f,and " ") (1
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  ){3}                     end of \1 (NOTE: because you are using a
                           quantifier on this capture,only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \(                       '('
--------------------------------------------------------------------------------
    LW                       'LW'
--------------------------------------------------------------------------------
    \)                       ')'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    [\d,*?\/-]+              any character of: digits (0-9),'?','-' (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  \s+                      whitespace (\n,and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    (?:                      group,but do not capture:
--------------------------------------------------------------------------------
      JAN                      'JAN'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      FEB                      'FEB'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      MAR                      'MAR'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      APR                      'APR'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      MAY                      'MAY'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      JUN                      'JUN'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      JUL                      'JUL'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      AUG                      'AUG'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      SEP                      'SEP'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      OCT                      'OCT'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      NOV                      'NOV'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      DEC                      'DEC'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      [\d,*?\/#-]+             any character of: digits (0-9),'#','-' (1 or more
                               times (matching the most amount
                               possible))
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    (?:                      group,but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      \s*                      whitespace (\n,and " ")
                               (0 or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------,'
--------------------------------------------------------------------------------
      \s*                      whitespace (\n,and " ")
                               (0 or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      (?:                      group,but do not capture:
--------------------------------------------------------------------------------
        JAN                      'JAN'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        FEB                      'FEB'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        MAR                      'MAR'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        APR                      'APR'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        MAY                      'MAY'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        JUN                      'JUN'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        JUL                      'JUL'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        AUG                      'AUG'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        SEP                      'SEP'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        OCT                      'OCT'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        NOV                      'NOV'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        DEC                      'DEC'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        [\d,'-' (1 or more
                                 times (matching the most amount
                                 possible))
--------------------------------------------------------------------------------
      )                        end of grouping
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  \s*                      whitespace (\n,and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (                        group and capture to \4:
--------------------------------------------------------------------------------
    (?:                      group,but do not capture:
--------------------------------------------------------------------------------
      SUN                      'SUN'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      MON                      'MON'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      TUE                      'TUE'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      WED                      'WED'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      THU                      'THU'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      FRI                      'FRI'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      SAT                      'SAT'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      \(                       '('
--------------------------------------------------------------------------------
      LW                       'LW'
--------------------------------------------------------------------------------
      \)                       ')'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      [\d*?\/#-]+              any character of: digits (0-9),'-' (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    )                        end of grouping
--------------------------------------------------------------------------------
    (?:                      group,but do not capture:
--------------------------------------------------------------------------------
        SUN                      'SUN'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        MON                      'MON'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        TUE                      'TUE'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        WED                      'WED'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        THU                      'THU'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        FRI                      'FRI'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        SAT                      'SAT'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        \(                       '('
--------------------------------------------------------------------------------
        LW                       'LW'
--------------------------------------------------------------------------------
        \)                       ')'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        [\d*?\/#-]+              any character of: digits (0-9),'-' (1 or more times
                                 (matching the most amount possible))
--------------------------------------------------------------------------------
      )                        end of grouping
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of \4
--------------------------------------------------------------------------------
  (                        group and capture to \5:
--------------------------------------------------------------------------------
    [\d,*\/#-]*              any character of: digits (0-9),'-' (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \5
--------------------------------------------------------------------------------
  $                        before an optional \n,and the end of the
                           string