任何人都可以建议我一些查找和解析日期的方法(任何格式,“Aug06”,“Aug2006”,“2008 年 8 月 2 日”,“2006 年 8 月 19 日”,“08-06”,“01-08- 06") 在 python 中。
我遇到了这个问题,但它是在 perl 中...... Extract inconsistently formatted date from string (date parsing, NLP)
任何建议都会有所帮助。
最佳答案
这会找到您的例句中的所有日期:
for match in re.finditer(
r"""(?ix) # case-insensitive, verbose regex
\b # match a word boundary
(?: # match the following three times:
(?: # either
\d+ # a number,
(?:\.|st|nd|rd|th)* # followed by a dot, st, nd, rd, or th (optional)
| # or a month name
(?:(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*)
)
[\s./-]* # followed by a date separator or whitespace (optional)
){3} # do this three times
\b # and end at a word boundary.""",
subject):
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
它绝对不是完美的并且容易错过一些日期(特别是如果它们不是英文的 - 21.Mai 2006
将失败,以及 4ème décembre 1999
) ,并匹配诸如 August Augst Aug
之类的废话,但由于在您的示例中几乎所有内容都是可选的,因此您在正则表达式级别上无能为力。
下一步是将所有匹配项输入解析器,看看它是否可以将它们解析为合理的日期。
正则表达式无法正确解释上下文。想象一个(愚蠢的)文本,如 You'll find it in box 21. August 3rd will be the shipping date.
它将匹配 21。 8 月 3 日
当然无法解析。
关于python - 如何在 Python 中使用 NLP、RegEx 查找句子中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3809985/