Как заменить специальный текст на другой специальный текст - Python
Как изменить специальную строку на другое значение, если значение появляется?
Мои переменные
special_url = http://127.0.0.1:8000/
text = '...'
special_symbol = {{URL}}
У меня есть такой текст:
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-
{{URL}}
Or-less normal distribution of letters, as opposed to using 'Content here, content here',
Мне нужно изменить текст выше на этот, когда значение {{URL}} находится в text
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-
http://127.0.0.1:8000/
Or-less normal distribution of letters, as opposed to using 'Content here, content here',
Я попробовал это, бот теперь работает:
massage = re.sub('{{URL}}', obj.special_url, text)
Если вам вообще не нужны регексы, в строках python есть встроенный метод .replace():
original = "link: {{URL}}"
special_symbol = "{{URL}}"
to_replace = "http://127.0.0.1:8000/"
new_string = original.replace(special_symbol, to_replace)
new_string # "link: http://127.0.0.1:8000/"