In this post you will see how to extract any date with python regular expression:

The list of the matched formats:

  • 10/10/2015
  • 10-10-15
  • 1 NOV 2010
  • 10 March 2015

You can watch a short video on this article here: python regular expression matching dates

You can be interested in Python project: Python useful tips and reference project

Regex Matching Date 10/10/2015

Regex date format: dd/mm/yyyy

  • [\d]{1,2} - match one or two digits
  • [\d]{4} - match exactly 4 digits
  • separator is /
import re

# Matching capital letters

str = """COBOL is a compiled English-like computer programming language designed for business use. 122. On 10/10/2015 is a big date unlike 1/11/2010 """

all = re.findall(r"[\d]{1,2}/[\d]{1,2}/[\d]{4}", str)

for s in all:
    print(s)

result

10/10/2015
1/11/2010

Regex Matching Date 10-10-15

Regex date format: dd-mm-yy

  • [\d]{1,2} - match one or two digits
  • separator is -
import re

# Matching capital letters

str = """COBOL is a compiled English-like computer programming language designed for business use. 122. On 10-10-15 is a big date unlike 1-11-10 """

all = re.findall(r"[\d]{1,2}-[\d]{1,2}-[\d]{2}", str)

for s in all:
    print(s)

result

10-10-15
1-11-10

Regex Matching Date 1 NOV 2010

Regular expression date format: dd MMM yyyy

  • [ADFJMNOS]\w* - Match big letter from ADFJMNOS followed by another letters
import re

# Matching capital letters

str = """COBOL is a compiled English-like computer programming language designed for business use. 122. On 10 OCT 2015 is a big date unlike 1 NOV 2010 """

all = re.findall(r"[\d]{1,2} [ADFJMNOS]\w* [\d]{4}", str) 

for s in all:
    print(s)

result

10 OCT 2015
1 NOV 2010

A more precise extraction for this example would be:

r"([\d]{1,2}\s(JAN|NOV|OCT|DEC)\s[\d]{4})"

result(if you change the OCT date):

('1 NOV 2010', 'NOV')

Better result for date extraction with this format can be done by using - ?: - check this code below:

all = re.findall(r"([\d]{1,2}\s(?:JAN|NOV|OCT|DEC)\s[\d]{4})", str)

result:
1 NOV 2010

Regular expression Matching Date 10 March 2015

Extracting date by Python regex which has full month name:

  • [ADFJMNOS]\w* - Match big letter from ADFJMNOS followed by another letters
import re

# Matching capital letters

str = """COBOL is a compiled English-like computer programming language designed for business use. 122. On 10 October 2015 is a big date unlike 1 November 2010 """

all = re.findall(r"[\d]{1,2} [ADFJMNOS]\w* [\d]{4}", str)

for s in all:
    print(s)

result

10 October 2015
1 November 2010

You can list the words that you want by using this regular expression:

all = re.findall(r"([\d]{1,2}\s(January|February|March|April|May|June|July|August|September|October|November|December)\s[\d]{4})", str)

result:

('10 October 2015', 'October')
('1 November 2010', 'November')

if you want your result to be only the dates then you can add - ?: to your middle group:

all = re.findall(r"(\d{1,2} (?:January|February|March|April|May|June|July|August|September|October|November|December) \d{4})", str)

result:

10 October 2015
1 November 2010