18

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?

3
  • perhaps use a capturing group Commented Nov 6, 2013 at 19:54
  • @Tommy: He is using a capturing group; that's what's causing the problem. When the regex has capturing groups, findall() returns only those, not the full match. Commented Nov 6, 2013 at 20:15
  • 1
    Mind you, you could also avoid the group entirely, changing (AM|PM) to [AP]M. Commented Dec 2, 2016 at 1:09

5 Answers 5

42

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

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

4

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)

Comments

2

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'

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.