Problem with parsing of cron expression in Java with specific listed words

274 views Asked by At

I have a cron expression with symbols words and digits:

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

And I have a regexp for parsing of this cron expression for Java:

^([\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}$

It works almost fine, but there is a problem. If I add an additional letter for any of words (for example: 'JAAAN') it also parses this cron expression. I need help. How can I solve it to parse only specific word in all my combination (as listed), such as 'JAN' or 'FEB' but not 'JANU' or 'FEBR'.

Please, I need help!

1

There are 1 answers

0
Ryszard Czech On

You did not provide enough data, so it won't be pretty:

^([\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,*\/#-]*)$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        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, \r, \t, \f, 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, \r, \t, \f, and " ")
                               (0 or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      ,                        ','
--------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, 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,*?\/#-]+             any character of: digits (0-9), ',',
                                 '*', '?', '\/', '#', '-' (1 or more
                                 times (matching the most amount
                                 possible))
--------------------------------------------------------------------------------
      )                        end of grouping
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, 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 (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ")
                               (0 or more times (matching the most
                               amount possible))
--------------------------------------------------------------------------------
      (?:                      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