Python получить несколько подстрок из строк

Имеется строка

Free space "974.62MB" in the Storage[/home/doom] has dropped  below the recommended threshold "5GB", it is recommended to free up space in the storage as soon as possible

Нужно получить из этой строки данные в кавычках и скобках. То есть: 974.62MB, /home/doom, 5GB. Как это можно сделать?


Ответы (1 шт):

Автор решения: Amgarak

Используйте регулярные выражения.

Пример:

import re

string = 'Free space "974.62MB" in the Storage[/home/doom] has dropped below the recommended threshold "5GB", it is recommended to free up space in the storage as soon as possible'

# Извлечение данных в кавычках
quoted_data = re.findall(r'"([^"]*)"', string)
# Извлечение данных в скобках
bracketed_data = re.findall(r'\[(.*?)\]', string)

print("Quoted data:", quoted_data)
print("Bracketed data:", bracketed_data)

введите сюда описание изображения

→ Ссылка