Небольшой трик с регулярными выражениями который редко вижу в чужом коде.
Допустим, вам нужно распарсить простой текст и вытащить оттуда пары имя+телефон. Вернуть всё это надо в виде списка словарей. Возьмем очень простой пример текста.
>>> 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
Nameless AOSP - Official | Android 13 | Nothing Phone (2)
📝 Details:
- Version: 1.4
- Status: #Official
- Device: Pong
- Released: 26/11/23
⬇️Download : Build l Images
👀 Changelogs : Here
🌳Device Tree : Here
📔Flashing Instructions : Here
🎙Support Group: Here
🏛 XDA : Here
📸 Screenshots:Here
💰Donate : Paypal | UPI : chandudyavanapelli3-1@okhdfcbank
🏷Tags : #AOSP#ROM#Nameless#T
❌Known Issues:
- No NFC Support (Sadly PN560 NFC is not supported in Android 13. Will be revisited once we move to Android 14)
✍️Note:
- Report issues (if any except known ones) with logs in support group / comments
- This is the final Android 13 release. We will start work on Android 14 from 2024 January.
🏆 Credits:
- Chandu Dyavanapelli for initial DT bringup
- Pong Dev and Testing Team & Contributors
👤 Maintainer : @Chandu_Dyavanapelli
🔔 Updates : @NothingPhone2Updates
🐙 Chat : @NothingPhone2
Nameless AOSP | Android 13 | Nothing Phone (2)
📝 Details:
- Version: 1.4
- Build: #Beta
- Status: #Official
- Device: Pong
- Released: 12/11/23]
⬇️Download : Build l Images
🌳Device Tree : Here
📔Flashing Instructions : Here
🎙Support Group: Here
📸 Screenshots:Here
💰Donate : Paypal
🏷Tags : #AOSP#ROM#Nameless#T
✨ Changelogs:
- Initial Test Build
❌ Known issues:
- NFC is broken, causing the system UI to crash after every reboot, but only once.
- Haptics are not similar to stock but will improve in upcoming builds.
- Dont use "Hey Google" as it leads to crashdump. Use factory reset to revive.
- Some device specific features are not added, will be added eventually.
- Report bugs (with logs ofc) in the comment section.
🏆 Credits:
- Chandu Dyavanapelli for initial DT bringup
- Pong Dev and Testing Team & Contributors
👤 Maintainer : @Chandu_Dyavanapelli
🔔 Updates : @NothingPhone2Updates
🐙 Chat : @NothingPhone2
Nameless CLO - Official | Android 15 | Nothing Phone (2)
🗒 Details:
- Version: A15
- Codename: #Pong
- Released: 22/12/24
🔽 Download: ROM [ Sourceforge l Mirror ] || Images [ Sourceforge l Mirror ]
🆘 Support : Telegram l Wiki
⚙️ Installation: Here
📸 Screenshots: Here
💵 Donate: Paypal | UPI: dyavanapellichandu@ybl
🔖 Tags : #ROM#CLO#Nameless#V#Official#NothingPhone2
✨Source Changelogs:
- Initial A15 Release based on CLO
✍️ Note:
- A clean flash is required if you're coming from Nameless-AOSP or any custom ROMs.
- You will not receive OTA updates in Nameless-AOSP for this build.
👤 Dev : Chandu Dyavanapell
🔔 Updates : @NothingPhone2Updates
💬 Chat : @NothingPhone2
Nameless AOSP - Official | Android 14 | Nothing Phone (2)
🗒 Details:
- Version: A14 - Final
- Codename: #Pong
- Released: 14/09/24
🔽 Download: Here | Mirror
🆘 Support : Here
⚙️ Installation: Here
📸 Screenshots: Here
💵 Donate: Paypal | UPI: dyavanapellichandu@ybl
🔖 Tags : #ROM#AOSP#Nameless#QPR3#U#Official#NothingPhone2
✨Source Changelogs:
- Merged September security patch
- Updated default wallpaper and boot animation
- Add navbar animation in pixel launcher
- Stability improvements and minor fixes
✍️ Note:
- This is the final A14 release and no OTA update exists for the same.
- Please update manually in recovery mode
👤 Dev : Chandu Dyavanapell
🔔 Updates : @NothingPhone2Updates
💬 Chat : @NothingPhone2