简体   繁体   English

正则表达式用于匹配各种日期

[英]RegEx for matching various dates

I am trying to put together a regex statement to match on each of the below date formats. 我试图将一个正则表达式声明放在一起以匹配以下每种日期格式。

* Mar 7, 2017
Mar. 7, 2017
* March 7, 2017
3-7-2017
03-07-2017
3-7-17
03-07-17
* 03/7/2017
* 03/07/17
* 3/7/17
Mar-07-2017
Mar-7-2017
March-07-2017

The below regex matches on the date formats that are indicated by an asterisk above. 下面的正则表达式与上面的星号指示的日期格式匹配。 I have tried in vain to add to what I already have but have been unsuccessful. 我徒劳地尝试添加我已经拥有的但未成功的内容。

([0-9]+)/([0-9]+)/([0-9]+)|([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))|\\w+\\s\\d{2},\\s\\d{4}|(?i)\\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec](?:ember)?)\\b (?:0?[1-9]|[1-2][0-9]|3[01]),? ([0-9] +)/([0-9] +)/([0-9] +)|([12] \\ d {3}-(0 [1-9] | 1 [0-2 ])-(0 [1-9] | [12] \\ d | 3 [01]))| \\ w + \\ s \\ d {2},\\ s \\ d {4} |(?i)\\ b(? :Jan(?:uary)?| Feb(?:ruary)?| Mar(?:ch)?| Apr(?:il)?| May | Jun(?:e)?| Jul(?:y)? | Aug(?:ust)?| Sep(?:tember)?| Oct(?:ober)?| Nov(?:ember)?| Dec](?: ember)?)\\ b(?:0?[ 1-9] | [1-2] [0-9] | 3 [01]),? \\d{4} \\ d {4}

Any help is always appreciated! 任何帮助总是感激不尽!

* Bonus question * *奖金问题*

On some occasions, there may be multiple date matches and I need it to find a match following a certain word. 在某些情况下,可能有多个日期匹配,我需要它来找到某个单词后的匹配。 In the past I've used the below syntax by enclosing the regex statement between the parenthesis after the period. 过去,通过在句点后的括号之间加上正则表达式,使用了以下语法。

(?<=Word).(StatementHere)

Try this then ... 试试这个然后...

([0-9]+)/([0-9]+)/([0-9]+)|((0?[1-9]|1[0-2])-(0?[1-9]|[12]\d|3[01])-(\d{4}|\d{2}))|\w+\s\d{2},\s\d{4}|(?i)\b(Jan(?:uary|\.)?|Feb(?:ruary|\.)?|Mar(?:ch|\.)?|Apr(?:il|\.)?|May|Jun(?:e|\.)?|Jul(?:y|\.)?|Aug(?:ust|\.)?|Sep(?:tember|\.)?|Oct(?:ober|\.)?|Nov(?:ember|\.)?|Dec(?:ember|\.)?)([ ](?:0?[1-9]|[1-2][0-9]|3[01]),?[ ]|-(?:0?[1-9]|[1-2][0-9]|3[01])-)(\d{4})

https://regex101.com/r/k1vaVN/1 https://regex101.com/r/k1vaVN/1

Readable version 可读版本

    ( [0-9]+ )                    # (1)
    /
    ( [0-9]+ )                    # (2)
    /
    ( [0-9]+ )                    # (3)
 |  
    (                             # (4 start)
         ( 0? [1-9] | 1 [0-2] )        # (5)
         -
         ( 0? [1-9] | [12] \d | 3 [01] )  # (6)
         -
         ( \d{4} | \d{2} )             # (7)
    )                             # (4 end)
 |  
    \w+ \s \d{2} , \s \d{4} 
 |  
    (?i)
    \b 
    (                             # (8 start)
         Jan
         (?: uary | \. )?
      |  Feb
         (?: ruary | \. )?
      |  Mar
         (?: ch | \. )?
      |  Apr
         (?: il | \. )?
      |  May
      |  Jun
         (?: e | \. )?
      |  Jul
         (?: y | \. )?
      |  Aug
         (?: ust | \. )?
      |  Sep
         (?: tember | \. )?
      |  Oct
         (?: ober | \. )?
      |  Nov
         (?: ember | \. )?
      |  Dec
         (?: ember | \. )?
    )                             # (8 end)
    (                             # (9 start)
         [ ] 
         (?: 0? [1-9] | [1-2] [0-9] | 3 [01] )
         ,? [ ] 
      |  -
         (?: 0? [1-9] | [1-2] [0-9] | 3 [01] )
         -
    )                             # (9 end)
    ( \d{4} )                     # (10)

update 更新
Just wrap the dates in a (?: ) group, then add whatever qualifier before 只需将日期包装在(?: ) :)组中,然后在前面添加任何限定符即可
it that you need. 您需要的。

word[ ]or[ ]phrase[ ]+\K(?:([0-9]+)/([0-9]+)/([0-9]+)|((0?[1-9]|1[0-2])-(0?[1-9]|[12]\d|3[01])-(\d{4}|\d{2}))|\w+\s\d{2},\s\d{4}|(?i)\b(Jan(?:uary|\.)?|Feb(?:ruary|\.)?|Mar(?:ch|\.)?|Apr(?:il|\.)?|May|Jun(?:e|\.)?|Jul(?:y|\.)?|Aug(?:ust|\.)?|Sep(?:tember|\.)?|Oct(?:ober|\.)?|Nov(?:ember|\.)?|Dec(?:ember|\.)?)([ ](?:0?[1-9]|[1-2][0-9]|3[01]),?[ ]|-(?:0?[1-9]|[1-2][0-9]|3[01])-)(\d{4}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM