Python regex operator OR

I am trying to map temporary formats in AM or PM.

ie 02:40PM 12:29AM 

I am using the following regex

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

but it only saves the string AM PM without numbers. What's wrong?

+6
source share
5 answers

Use a group without capture (?: And a link to a matching group.

Use re.I for case insensitivity.

 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

+15
source

Use a capture group without separators (?:...) :

 >>> from re import findall >>> mystr = """ ... 02:40PM ... 12:29AM ... """ >>> findall("\d{2}:\d{2}(?:AM|PM)", mystr) ['02:40PM', '12:29AM'] >>> 

Alternatively, you can shorten your regex to \d\d:\d\d(?:A|P)M

+3
source

It looks like you get access to group 1 when you need to access 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 through:

 timePattern.match('02:40PM').group(0) 
+3
source

You do not capture 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' 
+2
source

Have you accidentally captured the 1st cluster (material that matches the part of the template in parentheses) instead of the β€œ0th” cluster (which is a complete coincidence)?

+2
source

Source: https://habr.com/ru/post/957538/


All Articles