python - 我如何从这样的模式中提取时间?

标签 python regex datetime

我需要在文本上使用 datetime.strptime,如下所示。

“一些不确定长度的随机文本,1986 年 1 月 28 日”

我该怎么做?

最佳答案

您可能会发现 this问题有用。我会给出我在那里给出的答案,即使用 dateutil模块。这接受一个模糊参数,它将忽略任何看起来不像日期的文本。即:

>>> from dateutil.parser import parse
>>> parse("Some Random text of undetermined length Jan 28, 1986", fuzzy=True)
datetime.datetime(1986, 1, 28, 0, 0)

关于python - 我如何从这样的模式中提取时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/339856/

相关文章:

javascript - 如何在 Javascript 中格式化给定时区的日期?

javascript - 将时间字符串从 hhmm 转换为 hh :mm format in javascript

python argparse 数组而不是 str

html - Powershell 删除字符串内容中的 HTML 标签

python - 提取管道和日文字符之间的字母,并用逗号替换空格

java - Checkstyle Regexp 以捕获 Java 中的所有控制台打印

python - 将时间戳舍入到微秒 - pandas

python - scikit-learn 管道中的 transformer_weights 有什么用?

python - CNN 与 keras 预测能量

python 3 : When is a call to an instance attribute resolved as if it is an instance method?