Home > database >  Python: Extract time frame from string including time format (am/pm)
Python: Extract time frame from string including time format (am/pm)

Time:11-30

Writing Python code. Below are some sample strings containing day and time range

'Mon-Thu, Sun 11:30 am - 9 pm' 
'Sat-Sun 9:30 am - 9:30 pm'
'Sat 5 pm - 9 pm' 

I want to extract the time range from above including (am/pm) which will be

11:30am-9pm 
9:30am-9:30pm 
5pm-9pm

which I'm later planning to convert into 24hr format.

Things I tried which didn't work

# try-1
re.findall(r"(?i)(\d?\d:\d\d (?:a|p)m)", st) 

# try-2
re.findall(r"(?i)((\d?\d:\d\d|\d) (?:a|p)m)", st)

I'm not good at regex. Any help using regex or any other way will be appreciated.

CodePudding user response:

You can use

\b\d?\d(?::\d\d)?\s*[ap]m\s*-\s*\d?\d(?::\d\d)?\s*[ap]m\b

See the regex demo. Details:

  • (?i) - case insensitive flag
  • \b - a word boundary
  • \d?\d - one or two digits
  • (?::\d\d)? - an optional occurrence of : and two digits
  • \s* - zero or more whitespaces
  • [ap]m - a or p and then m
  • \s*-\s* - - enclosed with zero or more whitespaces
  • \d?\d(?::\d\d)?\s*[ap]m - same pattern as above
  • \b - word boundary

See a Python demo:

import re
text = '''Mon-Thu, Sun 11:30 am - 9 pm' 
'Sat-Sun 9:30 am - 9:30 pm'
'Sat 5 pm - 9 pm'''
time_rx = r'\d?\d(?::\d\d)?\s*[ap]m'
matches = re.findall(fr'\b{time_rx}\s*-\s*{time_rx}\b', text)
for match in matches:
    print("".join(match.split()))

Output:

11:30am-9pm
9:30am-9:30pm
5pm-9pm

CodePudding user response:

You were on the right track. Consider this version:

inp = ['Mon-Thu, Sun 11:30 am - 9 pm', 'Sat-Sun 9:30 am - 9:30 pm', 'Sat 5 pm - 9 pm']
output = [re.findall(r'\d{1,2}(?::\d{2})? [ap]m - \d{1,2}(?::\d{2})? [ap]m', x)[0] for x in inp]
print(output)  # ['11:30 am - 9 pm', '9:30 am - 9:30 pm', '5 pm - 9 pm']

CodePudding user response:

I don't know much about regex, but I can suggest this:

str = 'Mon-Thu, Sun 11:30 am - 9 pm'
for i in range(len(str)):
    if str[i].isdigit():
        new_str = str[i:]
        break
print(new_str)

This code is fine if you write code without them.

  • Related