Python regex match OR operator

I’m trying to match time formats in AM or PM.

i.e. 02:40PM
     12:29AM

I’m using the following regex

timePattern = re.compile('d{2}:d{2}(AM|PM)')

but it keeps returning only AM PM string without the numbers. What’s going wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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 dd:dd(?:A|P)M.

Method 3

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)

Method 4

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'

Method 5

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)?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x