Python regex match OR operator

Use a non capturing group (?: and reference to the match group.

Use re.I for case insensitive matching.

import re

def find_t(text):
    return re.search(r'\d{2}:\d{2}(?:am|pm)', text, re.I).group()

You can also use re.findall() for recursive matching.

def find_t(text):
    return re.findall(r'\d{2}:\d{2}(?:am|pm)', text, re.I)

See demo

Leave a Comment