Python regex match OR operator

后端 未结 5 822
轮回少年
轮回少年 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)
    
    0 讨论(0)
  • 2021-02-07 13: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

    0 讨论(0)
  • 2021-02-07 13:51

    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.

    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2021-02-07 13:54

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

    0 讨论(0)
提交回复
热议问题