Title: Match Dates
Slug: match_dates
Summary: Match Dates
Date: 2016-05-01 12:00
Category: Regex
Tags: Basics
Authors: Chris Albon

Based on: Regular Expressions Cookbook

Preliminaries


In [3]:
# Load regex package
import re

Create some text


In [13]:
# Create a variable containing a text string
text = 'My birthday is 09/15/1983. My brother\'s birthday is 01/01/01. My other two brothers have birthdays of 9/3/2001 and 09/1/83.'

Apply regex


In [15]:
# Find any text that fits the regex
re.findall(r'\b[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}\b', text)


Out[15]:
['09/15/1983', '01/01/01', '9/3/2001', '09/1/83']