Я согласен с ответом @ wiktor-stribiżew, но сделал рабочий пример.Я также сделал заметку в нижней части этой страницы учебника Google.
По сути, мы хотим заменить непоследовательные значения 'e', которые могут иметь букву в середине (пробел)для меня это означало бы отдельное слово и не соответствовало бы шаблону)что обратное верно.Мы хотим «захватить» и сохранить все, что находится между двумя буквами, заменяя их буквами «а».
В любом случае, вот мое решение:
import re
sstr = """
gere should be gara
cateral should remain cateral
"""
### Our pattern captures and preserves whatever is in between the e's
### Note that \w+? is non-greedy and looks for at least one word character between the e's.
regex = r'e(\w+?)e'
### We then sub out the e's and replace the middle with out capture group, which is group(1).
### Like \w, the backslash escapes the 1 for group-referencing purposes.
### If you had two groups, you could retain the second one with \2, and so on.
new_str = re.sub(regex, r'a\1a', sstr)
### Output answer to the terminal.
print(new_str)
Вывод:
gara should be gara
cateral should remain cateral