Use Regex re.sub to remove everything before and including a specified word

后端 未结 3 1145
执笔经年
执笔经年 2021-01-19 00:48

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

3条回答
  •  别那么骄傲
    2021-01-19 01:53

    You can use Lookahead:

    import re
    date_div = "Blah blah blah, Updated: Aug. 23, 2012"
    extracted_date = re.sub('^(.*)(?=Updated)',"", date_div)
    print extracted_date
    

    OUTPUT

    Updated: Aug. 23, 2012
    

    EDIT
    If MattDMo's comment below is correct and you want to remove the "Update: " as well you can do:

    extracted_date = re.sub('^(.*Updated: )',"", date_div)
    

提交回复
热议问题