How to Extract Time from a String Using Regular Expressions on Python

Asked 1 years ago, Updated 1 years ago, 55 views

Use regular expressions on Python to extract the time from a mixed time and Japanese string.

Only the time part of the time is extracted, and the whole thing doesn't go well.

import re

time_str = '10:19 Departure → 11:50 Arrival 1 hour 31 minutes'
times=re.findall(r'([01][0-9]|2[0-3]):[0-5][0-9]', time_str)
print(times)

['10', '11']

['10:19', '11:50']

Python 3.8.2 (on pyenv)
macOS 11.6.1

I am a beginner in both regular expressions and Stack Overflow, so please let me know if there are any deficiencies.Thank you for your cooperation.

python regular-expression

2022-09-30 11:44

3 Answers

The reason why the example in the questionnaire doesn't work as expected is that the group you are capturing using parentheses only has the first two digits.

For example, you can write roughly as follows:

import re

time_str = '10:19 Departure → 11:50 Arrival 1 hour 31 minutes'
times = re.search(r'([0-9]{1,2}:[0-9]{2}) originating →([0-9]{1,2}:[0-9]{2}) arrival', time_str)
if times:
    print(times.groups())

At this rate, a tuple will be printed, but if the list is better, use list(times.groups()).


2022-09-30 11:44

import re

time_str = '10:19 Departure → 11:50 Arrival 1 hour 31 minutes'
times=re.findall(r'(?:[01][0-9]|2[0-3]):[0-5][0-9]')', time_str)
print(times)#['10:19', '11:50']


2022-09-30 11:44

Regular expression syntax

(?:...)

This is a non-captured version of normal parentheses.It matches a regular expression enclosed in parentheses, but the partial string that this group matches cannot be retrieved after the match has been performed or referenced later in the pattern.

import re

time_str = '10:19 Departure → 11:50 Arrival 1 hour 31 minutes'
times=re.findall(r'(?:[01][0-9]|2[0-3]):[0-5][0-9]')', time_str)
print(times)

#
['10:19', '11:50']


2022-09-30 11:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.