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

Based on: StackOverflow

Preliminaries


In [24]:
# Load regex package
import re

Create some text


In [25]:
# Create a variable containing a text string
text = 'Chris: 12:34am. Steve: 16:30'

Apply regex


In [27]:
# Find any text that fits the regex
re.findall(r'([0-1]\d:[0-5]\d)\s*(?:AM|PM)?', text)


Out[27]:
['12:34', '16:30']