Python regex match OR operator

后端 未结 5 828
轮回少年
轮回少年 2021-02-07 13:11

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.compi         


        
5条回答
  •  心在旅途
    2021-02-07 13:54

    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'
    

提交回复
热议问题