有人可以给我写一个正则表达式,除了数字和斜线之外会删除所有内容吗?
例如,我有以下内容:
[u'Posted by Kendra E. on 3/17/2016', u'Posted by Jasmine B. on 3/16/2016', u'Posted by Chris H. on 3/17/2016', u'Posted by Katie S. on 3/17/2016', u'Posted by Samidha K. on 3/17/2016', u'Posted by Melissa W. on 3/20/2016', u'Posted by Travis S. on 3/18/2016', u'Posted by Lilla L. on 3/16/2016']
我想成功
['3/17/2016', '3/16/2016', '3/17/2016'...]
这可能吗?
谢谢, 本
答案 0 :(得分:0)
或者,您可以使用dateutil
parser将日期字符串直接提取到datetime对象中:
>>> from dateutil.parser import parse
>>> l = [u'Posted by Kendra E. on 3/17/2016', u'Posted by Jasmine B. on 3/16/2016', u'Posted by Chris H. on 3/17/2016', u'Posted by Katie S. on 3/17/2016', u'Posted by Samidha K. on 3/17/2016', u'Posted by Melissa W. on 3/20/2016', u'Posted by Travis S. on 3/18/2016', u'Posted by Lilla L. on 3/16/2016']
>>> [parse(item, fuzzy=True) for item in l]
[datetime.datetime(2016, 3, 17, 0, 0), datetime.datetime(2016, 3, 16, 0, 0), datetime.datetime(2016, 3, 17, 0, 0), datetime.datetime(2016, 3, 17, 0, 0), datetime.datetime(2016, 3, 17, 0, 0), datetime.datetime(2016, 3, 20, 0, 0), datetime.datetime(2016, 3, 18, 0, 0), datetime.datetime(2016, 3, 16, 0, 0)]
答案 1 :(得分:0)
您是要替换文本,还是只捕获除数字和斜线之外的所有内容?
This example匹配所有非数字或斜线,然后它捕获所有数字和斜杠。然后它只用捕获的值替换它。
请查看左侧的code generator
以获取此正则表达式模式的Python代码。