Some time ago
您想了解这些推文是何时发布的。阅读后,您发现日期的写法不统一。您决定使用 .findall() 提取日期,之后再统一格式,使它们看起来一致。
您注意到日期总是以下列方式之一出现:
27 minutes ago
4 hours ago
23rd june 2018
1st september 2019 17:25
列表 sentiment_analysis(包含 3 条推文的文本)以及模块 re 已加载到您的会话中。您可以在 IPython Shell 中使用 print() 查看数据。
本练习是课程的一部分
Python 中的正则表达式
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Complete the for loop with a regex to find dates
for date in sentiment_analysis:
print(re.____(r"\d{____}\s____\s____", date))