Функция sub в regex может принимать функцию в качестве аргумента repl.
📄 Из документации:
If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string.
То есть для каждого совпадения будет вызвана функция для вычисления замены вместо замены на одну и ту же строку для всех совпадений.
Иными словами, для замены разных совпадений на разные строки не потребуется запускать re.sub() много раз для каждой строки замены. Достаточно определить функцию, которая вернёт строку для каждого из совпадений.
Описание слишком запутанное🤔, давайте лучше рассмотрим на простом примере:
Создаем карту замены. То есть какие строки на какие требуется менять.
remap = {
'раз': '1',
'два': '2',
'три': '3',
'четыре': '4',
'пять': '5',
}
Пишем функцию поиска строки для замены. Единственным аргументом будет объект re.Match.
Используя данные этого объекта мы вычисляем замену on-the-fly!
def get_str(match: re.Match):
word = match.group(1)
return remap.get(word.lower()) or word
Пример текста.
text = '''Раз Два Три Четыре Пять
Вместе будем мы считать
Пять Четыре Три Два Раз
Мы считать научим вас
'''
Теперь запускаем re.sub и вместо строки замены (repl) подаём имя функции.
(Данный паттерн ищет отдельные слова в тексте)
>>> print(re.sub(r'(\w+)', get_str, text))
1 2 3 4 5
Вместе будем мы считать
5 4 3 2 1
Мы считать научим вас
Думаю, достаточно наглядно 🤓
#libs#regex
📰Group Appearance Settings
Upgrading your group will unlock new features for the group:
- Color and Profile Logo
- Group Status Emoji
- Group Theme and Wallpaper
- Group Emoji Pack
#Boost | #Groups
👉The TG Times
📰Group Custom Emoji Pack
Group Admins can set a Custom Emoji Pack for the group.
All members can use emojis from this pack in the group, even without ⭐️ Premium subscription.
#Boost | #Groups
👉The TG Times
📰Boost to send messages in the group
Group admins can choose how many boosts a user must give to the group to bypass restrictions on sending messages.
#Boost | #Groups
👉The TG Times
📰Boosting Groups
Changes in the configuration of Telegram for macOS show that it will be possible to boost groups.
After upgrading groups, the ability to set the background for the group, choose the status emoji and choose the emoji pack for the group will be added.
It seems that just as in groups, the special sticker pack for that group can be set, it will also be possible to choose the emoji pack.
Thanks to @tgcrawl
#Boost | #Groups
👉The TG Times
📰Boosting Groups
Changes in the configuration of Telegram for macOS show that it will be possible to boost groups.
After upgrading grouos, the ability to set the background for the group, choose the status emoji and choose the emoji pack for the group will be added.
It seems that just as in groups, the special sticker pack for that group can be set, it will also be possible to choose the emoji pack.
Thanks to @tgcrawl
#Boost | #Groups
👉The TG Times
📰Boosting Groups
Changes in the configuration of Telegram for macOS show that it will be possible to boost groups.
After upgrading grouos, the ability to set the background for the group, choose the status emoji and choose the emoji pack for the group will be added.
It seems that just as in groups, the special sticker pack for that group can be set, it will also be possible to choose the emoji pack.
Thanks to @tgcrawl
#Boost | #Groups
👉The TG Times
@TheNexasMusic_bot
Qué puede hacer este bot?
Con este bot puedes reproducir música nn grupos de telegram a través del chat de voz!
Idioma: Inglés
(visto en @BotsGram_cu)
#groups, #music
@Gramaphone_voice_bot
Qué puede hacer este bot?
Con este bot puedes reproducir música nn grupos de telegram a través del chat de voz!
Idioma: Inglés
(visto en @BotsGram_cu)
#groups, #music
@MultiForwardBot
Qué puede hacer este bot?
Este bot reenvía mensajes y medios de tu canal a grupos ilimitados de telegram!
Idioma: Inglés, Italiano, Español
(visto en @BotsGram_cu)
#channel, #groups