Skip to content

Instantly share code, notes, and snippets.

@miketahani
Last active January 16, 2023 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miketahani/77f091eee50ee476e81d to your computer and use it in GitHub Desktop.
Save miketahani/77f091eee50ee476e81d to your computer and use it in GitHub Desktop.
answer to a NICAR-L question
import re
from datetime import datetime as dt
fake_dates = '10-Jan-2011, 01-Feb-02, ##-Bla-##, 01-Bla-2014'
# you can probably do some lookahead assertion at the end of the regex to only capture 2 digits or the last 2 digits
# instead of the mess below [1]
date_finder = re.compile('(\d{2}-(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2,4})')
dates = re.findall(date_finder, fake_dates)
# two-digit date or four-digit date
digits = ['%d-%b-%y', '%d-%b-%Y']
# [1] this is "the mess below"
new_dates = [dt.strptime(date, digits[len(date) > 9]).strftime('%Y-%m-%d') for date in dates]
print new_dates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment