【发布时间】:2015-06-06 23:30:49
【问题描述】:
我有一堆带有随机文本的行,每行的末尾都有一个时间戳。我试图在时间戳之前拆分这些行。
当前输出:
Yes, I'd say so. Nov 08, 2014 UTC
Hell yes! Oct 01, 2014 UTC
Anbefalt som bare det, løp og kjøp. Sep 16, 2014 UTC
Etc.
所需的输出(“制表符”是指实际的空白):
Yes, I'd say so. <tab> Nov 08, 2014 UTC
Hell yes! <tab> Oct 01, 2014 UTC
Anbefalt som bare det, løp og kjøp. <tab> Sep 16, 2014 UTC
Etc.
到目前为止,我已经使用“替换”在月份之前放置一个制表符。像这样:
my_string.replace("May ", "\tMay ").replace("Apr ", "\tApr ").replace("Mar ", "\tMar ").replace("Feb ", "\tFeb ") etc. (incomplete code)
这很好用,除非随机文本涉及月份的名称,例如“我去年五月买的,很棒的东西”。由于日期是以这种特定方式格式化的,如果可能的话,我想用正则表达式和通配符对此进行改进。有没有办法在这些日期之前放置一个标签?如上所示,日期格式如下:
[Three-letter abbreviation of the month] [two-digit day] [,] [four-digit year] [UTC]
例如
Oct 31, 2014 UTC
请原谅业余代码和方法,我是一个绝对的正则表达式 n00b。我已经在 SO 上四处寻找答案,但我做得很短。希望有人能帮忙!
【问题讨论】:
-
每个人都想使用正则表达式!字符串操作通常有更简单的解决方案,如下面的解决方案所示。
标签: python regex python-2.7 replace data-cleaning