Функция 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
#PAWS Solana OG Allocation
If you have an active Solana wallet, check if you’ve received an airdrop or not.
Here's the full list of the 13 projects in the Solana OG Allocation:
Memecoins:
DogWifHat ($WIF) -
Bonk ($BONK)
Pudgy Penguins ($PENGU)
STONKS ($STNK)
GigaChad ($GIGA)
Eliza ($Ai16z)
DeFi:
Magic Eden ($ME)
Phantom Wallet
Jupiter ($JUP)
Raydium ($RAY)
Solana Phone
Jito ($JTO)
Mad Lads by Backpack
Paws Airdrop Checker: https://paws.community/app/solana-og
#PAWS
The airdrop criteria have appeared on the PAWS website !!
➡Open the PAWS app
➡Tap "Check Eligibility"
➡See if you qualify for the airdrop!
Are you Eligible?
🗿 Yes
😢 No
New Task added in @Paws
Get 5000 Paws by completing this task
👉
Big #PAWS Sighted In These Area! 🐶
New task just dropped.
Put 🐾 in your name and stay on marked trails!
👉 PLAY PAWS : CLICK
#Paws Bot is Verified !!
Just a few days ago, the @PAWS bot launched and within less then 3 days it reached a massive milestone of 10 million users! 🤯 And now, it's officially verified by Telegram! 🎉
👉PLAY : @PAWS
💎PAWS ONE LAST RIDE 💎
The final mandatory check is live!
Only those who finish it will stay in.
Go to PAWS Web & check it off.
P.S. Special roles for holding 10+ Vouchers are now available in PAWS Discord!🐾
#PAWS HIGHER 🐶
💎PAWS ONE LAST RIDE 💎
The final mandatory check is live!
Only those who finish it will stay in.
Go to PAWS Web & check it off.
P.S. Special roles for holding 10+ Vouchers are now available in PAWS Discord!🐾
#PAWS HIGHER 🐶
🔤🔤🔤🔤
Do you like detective stories and puzzles?
Let us analyze the picture and say what can you see and predict for the upcoming listing and TGE?
#Paws
#paws
🐾PAWS 18,000,000 million participants
🔥The project is gaining momentum, the next trail according to the hint in the video is the X Empire project, they should still drop coins for it💎
💵Easy money 10-50$, without doing anything
@PAWS
--------------------------------------------------
🤘Blums code
🗣#PAWS – Upcoming Airdrop for Solana DeFi Users
PAWS has announced an upcoming airdrop, rewarding everyone who has used DeFi on Solana!
🔗Post
💡 What to Expect:
🟠 Rewards for Jupiter swaps
🟠 Perks for NFT purchases on Magic Eden
🟠 Bonuses for using Phantom Wallet
⏳ Now is the time to prepare your accounts for the drop!
☝️ Start trading on Bybit
Crypto Drop Club 👉 SUBSCRIBE