Функция 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
🌎 The deepest point on land is the Bentley Subglacial Trench in Antarctica, buried beneath almost 3 kilometers of ice. This hidden valley lies 2,555 meters below sea level, far deeper than Death Valley, and remains inaccessible except through advanced radar technology. ✨
#Antarctica⚡#geology⚡#extremes
👉subscribe Interesting Planet
🌍 The Gobi Desert experiences freezing winters with temperatures dropping below –40°C, making it one of the few deserts known for both scorching heat and extreme cold. ✨
#deserts⚡#climate⚡#extremes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌎 High-altitude deserts like the Atacama in Chile are among Earth’s driest extreme climate zones, receiving less than 2 millimeters of rain a year. These parched regions are so arid that some weather stations have never recorded rainfall. ✨
#climate⚡#desert⚡#extremes
👉subscribe Interesting Planet
🌍 Antarctica’s interior is the only place on Earth with a true polar climate—temperatures rarely rise above freezing, and less precipitation falls each year than in most deserts. ✨
#climatezones⚡#antarctica⚡#extremes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Earth holds the record for the highest known surface temperatures for a rocky planet in our Solar System, with some desert spots topping 56°C—hotter than the ground on Venus or Mars. ✨
#planet⚡#deserts⚡#extremes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌎 Rising in the Peruvian Andes at 5,100 meters, La Rinconada is the world’s highest permanent settlement. Over 40,000 people live here, mostly mining gold in challenging conditions with no running water or sewage system. ✨
#settlements⚡#geography⚡#extremes
👉subscribe Interesting Planet
👉more Channels
🌎 Life thrives in Earth’s extreme climate zones, like the frigid polar regions and the hyper-arid Atacama Desert. The Atacama receives less than 2 mm of rain annually, making it one of the driest places on Earth, while Antarctica sees winter temperatures plunge below –60°C. ✨
#climate⚡#extremes⚡#geography⚡#adaptation
👉subscribe Interesting Planet
👉more Channels
🌍 The Lut Desert in Iran holds the record for Earth’s hottest land temperature ever measured from space—an astonishing 80.8°C, far exceeding typical desert heat. ✨
#deserts⚡#temperature⚡#extremes⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🪐 In 2016, astronomers using the Hubble Space Telescope observed the exoplanet WASP-121b, where temperatures in its stratosphere soar above 2,500°C and molecules such as water vapor exist as glowing, superheated gas. The planet’s intense gravity causes it to stretch into a teardrop shape, while metals like iron and magnesium escape its atmosphere, streaming into space in a shimmering trail. ✨
#exoplanets⚡#atmosphere⚡#extremes
👉subscribe Universe Mysteries
🌎 The coldest permanently inhabited place is Oymyakon, Russia, where winter temperatures drop below −50°C. The hottest surface temperature recorded was 80.8°C in Iran’s Lut Desert in 2005. ✨
#climate⚡#extremes⚡#Earth⚡#geography
👉subscribe Interesting Planet
🌎 Nestled in the Siberian taiga, the Russian town of Verkhoyansk is one of the coldest inhabited places on Earth. Winter temperatures can drop below –50°C, and the town recorded a range of over 105°C between its lowest and highest temperatures—a world record for temperature variation in a single settlement. ✨
#cities⚡#Siberia⚡#extremes
👉subscribe Interesting Planet
👉more Channels
🪐 Exoplanet 2MASS J2126–8140 holds the record for the widest known orbit around its star—about 1 trillion kilometers away, or nearly 7,000 times the distance from Earth to the Sun. This gas giant drifts so far from its host that a single "year" there lasts nearly 900,000 Earth years, exposing it to the coldest, loneliest planetary conditions ever measured. ✨
#exoplanets⚡#extremes⚡#space⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos
👉subscribe Universe Mysteries
👉more Channels