Home > Software engineering >  How to capture the time from this string with regex
How to capture the time from this string with regex

Time:09-27

I have this string <td>pending since<br/>Thu 15 Sep 2022 11:43:49 PM UTC </td> And I want to capture the "Thu 15 Sep 2022 11:43:49 PM UTC" in a regex.

I've tried re.compile('<td>pending since<br/>* </td>') To no avail.

CodePudding user response:

I might use a regex find approach here seeking the timestamp:

inp = "<td>pending since<br/>Thu 15 Sep 2022 11:43:49 PM UTC </td>"
ts = re.findall(r'\b\w{3} \d{1,2} \w{3} \d{4} \d{2}:\d{2}:\d{2} [AP]M \w ', inp)
print(ts)  # ['Thu 15 Sep 2022 11:43:49 PM UTC']

CodePudding user response:

Regex is the way!

import re

time = re.findall(r'(\d{2}:\d{2}:\d{2})', test_string_goes_here)[0]
print(time)

  • Related