Я пробовал следующее:
Код: Выделить всё
import re
from itertools import count
text = "Hello_Dear_Today_is_Nice_Today_is_Nice"
def replace_nth(text, sub, rep_sub, n):
c = count(0)
res = re.sub(r"{}".format(sub), lambda x: rep_sub if next(c) == n else x.group(), text)
return res
print(replace_nth(text, "Today", "Today_2", 2))
Я ожидаю:
Код: Выделить всё
Hello_Dear_Today_is_Nice_Today_2_is_NiceПодробнее здесь: https://stackoverflow.com/questions/784 ... -itertools