Небольшой трик с регулярными выражениями который редко вижу в чужом коде.
Допустим, вам нужно распарсить простой текст и вытащить оттуда пары имя+телефон. Вернуть всё это надо в виде списка словарей. Возьмем очень простой пример текста.
>>> text = '''
>>> Alex:8999123456
>>> Mike:+799987654
>>> Oleg:+344456789
>>> '''
Соответственно, для выделения нужных элементов будем использовать группы. Получится такой паттерн:
(\w+):([\d+]+)
Как мы будем формировать словарь из найденных групп?
>>> import re
>>> results = []
>>> for match in re.finditer(r"(\w+):([\d+]+)", text):
>>> results.append({
>>> "name": match.group(1),
>>> "phone": match.group(2)
>>> })
>>> print(results)
[{'name': 'Alex', 'phone': '8999123456'}, ...]
Можно немного сократить запись используя zip
>>> results = []
>>> for match in re.finditer(r"(\w+):([\d+]+)", text):
>>> results.append(dict(zip(['name', 'phone'], match.groups())))
Но есть способ лучше! Это именованные группы в regex. Можно в паттерне указать имя группы и результат сразу забрать в виде словаря.
>>> for match in re.finditer(r"(?P<name>\w+):(?P<phone>[\d+]+)", text):
>>> results.append(match.groupdict())
То есть всё что я сделал, это добавил в начале группы (внутри сбокочек) такую запись:
(?P<group-name>...)
Теперь найденная группа имеет имя и можно обратиться к ней как к элементу списка
>>> name = match['name']
Либо забрать сразу весь словарь методом groupdict()
>>> match.groupdict()
#tricks#regex
#ARPA/USDT analysis :
#ARPA is currently moving upward within a bullish channel, consistently forming higher highs (HHs) and higher lows (HLs). The price has recently rebounded from the support of the channel and is expected to test the resistance level of the channel soon.
TF : 4H
Entry : $0.05590
Target : $0.06165
SL : $0.05187
#ARPA/USDT Analysis-
ARPA has formed bullish symmetrical triangle pattern. Once it will break the high then it has high potential to go up till the target zone.
T.F.- 4-H
ENTRY- as soon as it gives breakout
SL- 0.04610
TARGET- 0.05500
Note: If the stop-loss is triggered before entry, disregard the trade as the price action may develop differently.
#ARPA/USDT analysis -
#ARPA is making new lows, the price is currently consolidating near the resistance zone. Wait for a pullback towards the resistance zone and wait for the price to be rejected from the zone for a short entry. The previous low is expected to be tested.
TF : 2h
Entry : $0.0486
Target : $0.0442
SL : $0.0506
#ARPA/USDT ANALYSIS
ARPA is currently consolidating within a descending triangle, with a rebound occurring from the horizontal demand zone at the base of the pattern.
The Ichimoku Cloud serves as a resistance overhead, situated above the triangle. A decisive move, either a breakout or breakdown, is needed before considering any trades.