How do we use Python regular expression to match a date string?



Regular expressions, or regex, are useful in Python when you need to find patterns in text. If you want to perform verification for a string that contains a date, in this case regex is useful.

In this article, we will learn simple ways to match date strings using Python regex in the coming sections. Dates can come in many formats, like -

  • YYYY-MM-DD
  • YYYY/MM/DD
  • YYYY.MM.DD
  • YYYYMMDD
  • DD-MM-YYYY
  • DD/MM/YYYY

Regular expression helps us find these patterns easily, so we can validate or extract dates from the given text. There are several ways to match a date string using Python regex, such as -

Basic Date Match (Year-Month-Day)

The following example will use Python regex to match a date string in the format YYYY-MM-DD. We are using \d{4} for year and \d{2} for month and day. Check the below example -

# import re module
import re

def match_date(text):
    pattern = r"\d{4}-\d{2}-\d{2}"
    return re.search(pattern, text)

# Test the function
print(match_date("Today is 2025-05-13"))

The result of this Python code is -

<re.Match object; span=(9, 19), match='2025-05-13'>

Match Date with Slashes (DD/MM/YYYY)

The following example will use Python regex to match a date string in the format DD/MM/YYYY. We are using \d{2} for day and month and \d{4} for year. Here is the program -

# import re module
import re

# Function to match date in DD/MM/YYYY format   
def match_date_slash(text):
    pattern = r"\d{2}/\d{2}/\d{4}"
    return re.search(pattern, text)

# Test the function
print(match_date_slash("Date: 12/05/2025"))

This will create the following outcome -

<re.Match object; span=(6, 16), match='12/05/2025'>

Match Date with Text

This program matches and displays the month and day from the given string, which may contain a date. So we have created a fmd() which uses regex to find a month name followed by a day number and prints the details.

import re

def fmd(s):
    # Pattern to match month name and day
    r = r"([a-zA-Z]+) (\d+)"   
    
    # Use search instead of match
    m = re.search(r, s)         
    
    if m is None:
        print("Invalid input")
        return
    
    print("Data: %s" % m.group())
    print("Month: %s" % m.group(1))
    print("Day: %s" % m.group(2))
    
# Test the function
fmd("Sep 18")
print("")
fmd("I was born on September 18")

This will lead to the following outcome -

Data: Sep 18
Month: Sep
Day: 18

Data: September 18
Month: September
Day: 18

Match Date Using strptime() Function

We can use the strptime() function from the datetime module to check if a string matches a specific date format.

It parses a string into a datetime object as per the given format. If the string does not match the format, it raises a ValueError. Here is an example -

# Use strptime() function 
from datetime import datetime

# initialize string
my_str = '18-10-1990'

# print original string
print("Original string is as follows: " + str(my_str))

# initialize format
format = "%d-%m-%Y"

# initialize result
res = True

# try-except block to check if the string matches the format
try:
    res = bool(datetime.strptime(my_str, format))
except ValueError:
    res = False

# printing result
print("String matches the format: " + str(res))

This will generate the following result -

Original string is as follows: 18-10-1990
String matches the format: True

Match Date Using dateutil.parser.parse()

Here we are using the dateutil module, which offers a powerful parser that can parse dates in various formats. We can use the parse() function from the dateutil.parser module to check if a string matches a specific date format. Here is an example -

# using dateutil.parser.parse()
from dateutil import parser

# initialize string
my_str = '09-01-1998'

# print original string
print("Original string is as follows : " + my_str)

# format to check
format = "%d-%m-%Y"

# Initialize result
res = True

# using try-except block
# to check if string matches
try:
    res = bool(parser.parse(my_str))
except ValueError:
    res = False

# printing result
print("Does the string match the date format ? : " + str(res))

This will produce the following result -

Original string is as follows : 09-01-1998
Does the string match the date format ? : True
Updated on: 2025-06-16T12:32:16+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements