Функция 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
🆕 TokenPocket Chrome Extension's Latest (1.2.3) Version has enabled Nostr Protocol by default! #Nostr
👉https://extension.tokenpocket.pro/#/
✅With no complex setting steps, all the TokenPocket users can get their own Nostr address easily!
🎯Your Points Matter! TokenPocket will keep working on listening to communities' voices and optimizing users' experience. #TokenPocket#nostr
How to enable Nostr Protocol on TokenPocket Mobile wallet?
Guide 👉https://help.tokenpocket.pro/en/wallet-operation/protocol/nostr
【Details】https://twitter.com/TokenPocket_TP/status/1745755553039802879
【Powered By】Crypto Box
🔵Enjoy Nostr Protocol on TokenPocket_TP.
TokenPocket now supports #Nostr Protocol on Mobile Wallet and Chrome Extension Wallet. You can easily enable your #Nostr address on #TokenPocket!
Guide👇
https://tokenpocket-gm.medium.com/enjoy-nostr-protocol-on-tokenpocket-b53044dd10f0
【Details】https://twitter.com/TokenPocket_TP/status/1733450921391358056
【Powered By】Crypto Box
#Nostr Protocol is now available on TokenPocket_TP!
🔥TokenPocket now provides the Nostr support for #Bitcoin, dogecoin network, and all the EVM-compatible networks!
How to enable Nostr in your TokenPocket wallet?
🔵Android: Tap “Me” - “Experience” - Enable Nostr. You can get your Nostr address by tapping “Details” on the Assets Page.
🔵iOS: Tap “Me” - “Setting” - Turn on “Nostr”. You can get your Nostr assets by tapping “Details” on the Assets page.
#taproot#bitcoin#Nostr
【Details】https://twitter.com/TokenPocket_TP/status/1724712691523100844
【Powered By】Crypto Box
🌟Nostr Protocol is Live On TokenPocket!
>Both on Mobile Wallet and Chrome Extension.
And Nostr Protocol is available for all the EVM-compatible networks, #Bitcoin, and also #Dogecoin on TokenPocket!
🔥Nostr is a simple, open protocol for a global, decentralized, and censorship-resistant social networking system.
📝Here is the tutorial to guide you on how to enable/support #Nostr on your TokenPocket wallet.
👉https://help.tokenpocket.pro/en/wallet-operation/protocol/nostr
🔥#Nostr Protocol is Live On TokenPocket_TP!
>Both on Mobile Wallet and Chrome Extension.
🔥Nostr is a simple, open protocol for a global, decentralized, and censorship-resistant social networking system.
Here is the tutorial to guide you on how to enable/support #Nostr on your TokenPocket wallet.
👉https://help.tokenpocket.pro/en/wallet-operation/protocol/nostr
📣All in TokenPocket! #Nostr#EnableNostr#Bitcoin
👉https://www.tokenpocket.pro/
👉https://extension.tokenpocket.pro/ NoScription
【Details】https://twitter.com/TokenPocket_TP/status/1741998782320136275
【Powered By】Crypto Box
#Bitcoin Eco is on fire!!🔥🔥🔥
TokenPocket Mobile Wallet now provides a super easy-to-use experience for most #Nostr-based DApps. Recently, we also noticed that there is a new product live on #Nostr Protocol. NostrAssets
📣You can discover Nostr Assets on NostrAssets with #TokenPocket and access the new era of #Bitcoin Eco.
> Enter "NostrAssets" on your Discover page then you can find it! ✅✅
Just the beginning!
💥 Let's work together and bring more highlights to #Bitcoin! #Taproot
❓How to enable #Nostr on your TokenPocket?
Tutorial👉https://help.tokenpocket.pro/en/wallet-operation/protocol/nostr
【Details】https://twitter.com/TokenPocket_TP/status/1719933875059855422
【Powered By】Crypto Box
#AMA TokenPocket × NostrSwap
⏰Jun 29th, 1 PM UTC
🪑 Venue: https://t.me/tokenPocket_en
✅Comment your Qs about NostrSwap, #BRC20 and #Nostron Twitter before AMA.
✅Join Telegram and enjoy the live AMA
🎁100 USDT to 5 best Twitter Qs & 5 best Live Qs
Zapstore
Permissionless and social app store built on the #nostr protocol
For #Android#Linux and #Mac
https://zapstore.dev
Download
https://zapstore.dev/download/
Sources
https://github.com/zapstore
Secure by default
Everything you install comes from trusted sources and is cryptographically verified. Not satisfied with the default? Choose your own curators, or mix and match! Censorship has no place in our world.
Relevant to you
Supercharged by the nostr open protocol, recommendations come right from your social connections — or use it in totally private way. With our growing catalog, you will never run out of great apps.
Support your devs
Connect with your favorite developers: send them direct feedback, request features and support them through micropayments, all without any middlemen.
#apk#store