我正在尝试将正则表达式声明放在一起以匹配以下每种日期格式。
* 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
下面的正则表达式与上面的星号指示的日期格式匹配。我徒劳地尝试添加我已经拥有的但未成功的内容。
([[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}
任何帮助总是值得赞赏的!
*奖励问题*
在某些情况下,可能有多个日期匹配,我需要它来找到某个单词后的匹配。过去,通过在句点后的括号之间加上正则表达式,使用了以下语法。
(?<=Word).(StatementHere)
答案 0 :(得分:2)
然后尝试一下...
([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
( [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)
更新
只需将日期包装在(?: )
组中,然后在之前添加任何限定符
您需要的。
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}))