Python regex match OR operator

后端 未结 5 889
轮回少年
轮回少年 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:36

    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)
    

提交回复
热议问题