【发布时间】:2019-08-07 07:41:27
【问题描述】:
我必须使用正则表达式从字符串中识别不同的日期格式,如下所示。
date can contain 21/12/2018
or 12/21/2018
or 2018/12/21
or 12/2018
or 21-12-2018
or 12-21-2018
or 2018-12-21
or 21-Jan-2018
or Jan 21,2018
or 21st Jan 2018
or 21-Jan-2018
or Jan 21,2018
or 21st Jan 2018
or Jan 21, 2018
or Jan 21, 2018
or 2018 Dec. 21
or 2018 Dec 21
or 21st of Jan 2018
or 21st of Jan 2018
or Jan 2018
or Jan 2018
or Jan. 2018
or Jan, 2018
or 2018
[should recognize (year only), (year and month), (year, month and day), year is mandatory in every date format to be recognized]
[months are abbreviated to three letters, first letter capital]
我的正则表达式如下,
\b(((((0?[1-9]|[12][0-9]|3[01])(\s*(st|nd|rd|th)?\s*(of)?\s*)?)|(20[012]\d)|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))[\/\-\.\,\s]*){1,3})\b
它没有按预期工作,并且它也有其他模式。我必须在要识别的每个日期模式中识别三个模式(year only)、(year and month)、(year, month and day)、必须是年份。
需要哪些更正才能使其正常工作?请帮忙。
【问题讨论】:
-
我没有投反对票,但我确实标记为过于广泛。你需要写一个很长的正则表达式替换。
-
正则表达式对于解决有这么多
ors 的问题是一个糟糕的选择。我认为你最好写一个解析器。 -
21-12-2018 or 12-21-2018– 12 月 11 日你打算做什么? -
@enumiro,这些日期来自不同公司的 10-k 文档的列标题,我正在尝试抓取。所以无法控制输入日期格式。