Regex for dates in Python: A guide

Hello love Python.-Developer,

it is not uncommon that we have to process dates in our daily work as developers. There are many libraries that can help with this, but sometimes using regular expressions (regex) is the easiest method. In this blog post, I would like to present several regex patterns to extract dates in different formats.

Why Regex?

Regular expressions provide a fast and efficient way, Patterns in texts to recognize. For example, when using a date format in a large Amount of text Regex is often the fastest solution, even though NLP Models naturally offer special opportunities.

Regex for different date formats

MM/DD/YYYY or MM-DD-YYYY

\b(0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01])[-/](19\d\d|20\d\d)\b

DD/MM/YYYY or DD-MM-YYYY

\b(0[1-9]|[12]\d|3[01])[-/](0[1-9]|1[0-2])[-/](19\d\d|20\d\d)\b

YYYY/MM/DD or YYYY-MM-DD

\b(19\d\d|20\d\d)[-/](0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01])\b

MM-DD-YY or MM/DD/YY

\b(0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01])[-/](\d\d)\b

YYYY-MM-DD (ISO 8601)

\b(19\d\d|20\d\d)-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])\b

Python code example

Here is a simple example of how to use these regex patterns in Python. can use:

import re
def find_dates(text, pattern):
    return re.findall(pattern, text)
text = "The date is 09/02/2023 and the other date is 12-12-2024."
pattern = r"\b(0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01])[-/](19\d\d|20\d\d)\b"
dates = find_dates(text, pattern)
print("Dates found:", dates)

Closing words

Recognizing dates with regex in Python is a valuable skill that can save you a lot of time. Note, however, that these patterns are not optimized for months with less than 31 days or leap years, since regex can only implement fixed rules. For more complex requirements, you should use a specialized date library like dateutil or pandas or the Konfuzio SDK consider.

I hope this guide was helpful! If you have any questions or suggestions, feel free to leave a comment.

Until next time!

"
"
Florian Zyprian Avatar

Latest articles