Created
January 21, 2020 17:07
-
-
Save Plaudenslager/06864f923776525464cb2c204ef06793 to your computer and use it in GitHub Desktop.
Python Date RegEx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These are for a Pandas Series, but regex portion works for any text | |
# Match dates like MM/DD/YYY with 1 or 2 digit month and date, and 2 or 4 digit year, and either / or - separators | |
df.str.findall(r'\d{1,2}[-/]\d{1,2}[-/]\d{2,4}') | |
# Match dates like 24 Jan 2001 with 1 or 2 digit day, and 2 or 4 digit year, full or abbreviated month | |
# with a possible period and / or coma after the month | |
df.str.findall(r'\d{1,2} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z.,]* \d{2,4}') | |
# Same as above, but with possibility for date to be before or after month, like Jan 24, 2001 | |
df[311:380].str.findall(r'(?:\d{1,2} )(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z.,]* (?:\d{1,2} )?\d{2,4}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment