Skip to content

Instantly share code, notes, and snippets.

@fmitha
Created November 1, 2020 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmitha/7f43e911e6841a3a3e8ed3ca4c104b00 to your computer and use it in GitHub Desktop.
Save fmitha/7f43e911e6841a3a3e8ed3ca4c104b00 to your computer and use it in GitHub Desktop.
import re
text = "The stable release of python 3.8 was on 05 Aug 2020. The stable release of C++17 was on 12 Jan 2019."
#date_pattern = re.compile(r'(\d+)( )+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)( )+(\d+)')
date_pattern = re.compile(r'(\d+) +(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) +(\d+)')
def format_date(date_input):
month_digit = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}
month_d = month_digit[date_input.group(2)]
return '{}-{}-{}'.format(date_input.group(3), month_d, date_input.group(1))
print(date_pattern.sub(format_date, text))
@adeak
Copy link

adeak commented Nov 1, 2020

text = "The stable release of python 3.8 was on 05 Aug 2020. The stable release of C++17 was on 12 Jan 2019." 
 
date_pattern = re.compile(r'(\d+)( )+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)( )+(\d+)') 
#date_pattern = re.compile(r'(\d+) +(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) +(\d+)') 
 
def format_date(date_input): 
    month_digit = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}
    month_d = month_digit[date_input.group(3)] 
    return '{}-{}-{}'.format(date_input.group(5), month_d, date_input.group(1)) 
 
print(date_pattern.sub(format_date, text))                                                                                                             

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment