Извлечение текущего времени из пользовательской команды: методы и пример кода

import re
import datetime
user_command = "Please tell me the current time."
# Regular expression pattern to extract time information
pattern = r'\btime\b'
# Find the match in the user command
match = re.search(pattern, user_command)
if match:
    # Get the current time
    current_time = datetime.datetime.now().time()

    # Format the time as a string
    formatted_time = current_time.strftime("%H:%M:%S")

    print(f"The current time is: {formatted_time}")
else:
    print("No time information found in the user command.")

В этом примере мы используем модуль reв Python для поиска слова «время» в строке user_command. Если совпадение найдено, мы используем модуль datetime, чтобы получить текущее время и отформатировать его как строку.