使用 Regex re.sub 删除指定单词之前和包括的所有内容 [英] Use Regex re.sub to remove everything before and including a specified word
本文介绍了使用 Regex re.sub 删除指定单词之前和包括的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,看起来像Blah blah blah, Updated: Aug. 23, 2012",我想使用正则表达式从中提取日期 Aug.2012 年 2 月 23 日
.我在堆栈中发现了一篇类似的文章: regex to remove all字符前的文本,但是当我尝试时这也不起作用
I've got a string, which looks like "Blah blah blah, Updated: Aug. 23, 2012", from which I want to use Regex to extract just the date Aug. 23, 2012
. I found an article in the stacks which has something similar: regex to remove all text before a character, but that's not working either when I tried
date_div = "Blah blah blah, Updated: Aug. 23, 2012"
extracted_date = re.sub('^[^Updated]*',"", date_div)
我怎样才能删除包括更新在内的所有内容,以便只有 Aug.2012年2月23日
还剩多少?
How can I remove everything up to and including Updated, so that only Aug. 23, 2012
is left over?
谢谢!
推荐答案
在这种情况下,您可以不使用正则表达式,例如:
In this case, you can do it withot regex, e.g:
>>> date_div = "Blah blah blah, Updated: Aug. 23, 2012"
>>> date_div.split('Updated: ')
['Blah blah blah, ', 'Aug. 23, 2012']
>>> date_div.split('Updated: ')[-1]
'Aug. 23, 2012'
这篇关于使用 Regex re.sub 删除指定单词之前和包括的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文