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
Use a non-delimited capture group (?:...)
:
>>> from re import findall
>>> mystr = """
... 02:40PM
... 12:29AM
... """
>>> findall("\d{2}:\d{2}(?:AM|PM)", mystr)
['02:40PM', '12:29AM']
>>>
Also, you can shorten your Regex to \d\d:\d\d(?:A|P)M
.
It sounds like you're accessing group 1, when you need to be accessing group 0.
The groups in your regex are as follows:
\d{2}:\d{2}(AM|PM)
|-----| - group 1
|----------------| - group 0 (always the match of the entire pattern)
You can access the entire match via:
timePattern.match('02:40PM').group(0)
You're not capturing the Hour, minute fields:
>>> import re
>>> r = re.compile('(\d{2}:\d{2}(?:AM|PM))')
>>> r.search('02:40PM').group()
'02:40PM'
>>> r.search('Time is 12:29AM').group()
'12:29AM'
Are you accidentally grabbing the 1st cluster (the stuff in that matches the portion of the pattern in the parentheses) instead of the "0st" cluster (which is the whole match)?