正则表达式识别所有日期

时间:2014-08-23 03:48:28

标签: java regex

我正在尝试识别格式DAY MONTH YEAR, DAY MONTH, MONTH YEAR and YEAR

的所有可能日期表达式

我的正则表达式:[0-9]{0,2}([Jj]anuary | [Ff]ebruary | [Mm]arch | [Aa]pril | [Mm]ay | [Jj]une | [Jj]uly | [Aa]ugust | [Ss]eptember | [Oo]ctober | [Nn]ovember | [Dd]ecember ){0,1}[0-9]{4} 缺少表达式DAY MONTH

我该如何解决?

1 个答案:

答案 0 :(得分:2)

尝试下一个:

(?i)(?<=\s|^)((\d{1,2} )?(january|february|march|april|may|june|july|august|september|october|november|december)( \d{4})?)|\d{4}(?=\s|$)

您可能希望在http://rick.measham.id.au/paste/explain.pl

中查看和解释此常规表达
NODE                     EXPLANATION
----------------------------------------------------------------------------
  (?i)                     set flags for this block (case-
                           insensitive) (with ^ and $ matching
                           normally) (with . not matching \n)
                           (matching whitespace and # normally)
----------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
----------------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------------
    ^                        the beginning of the string
----------------------------------------------------------------------------
  )                        end of look-behind
----------------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------------
    (                        group and capture to \2 (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------------
      \d{1,2}                  digits (0-9) (between 1 and 2 times
                               (matching the most amount possible))
----------------------------------------------------------------------------
                               ' '
----------------------------------------------------------------------------
    )?                       end of \2 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \2)
----------------------------------------------------------------------------
    (                        group and capture to \3:
----------------------------------------------------------------------------
      january                  'january'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      february                 'february'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      march                    'march'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      april                    'april'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      may                      'may'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      june                     'june'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      july                     'july'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      august                   'august'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      september                'september'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      october                  'october'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      november                 'november'
----------------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------------
      december                 'december'
----------------------------------------------------------------------------
    )                        end of \3
----------------------------------------------------------------------------
    (                        group and capture to \4 (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------------
                               ' '
----------------------------------------------------------------------------
      \d{4}                    digits (0-9) (4 times)
----------------------------------------------------------------------------
    )?                       end of \4 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \4)
----------------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------------
 |                        OR
----------------------------------------------------------------------------
  \d{4}                    digits (0-9) (4 times)
----------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
----------------------------------------------------------------------------
  )                        end of look-ahead