human
How to find and replace nth occurence of word in a sentence using python regular expression?
assistant
Here is some code to find and replace 2nd occurrence of word 'cat' by 'Bull' in a sentence 's'

re.sub('^((?:(?!cat).)*cat(?:(?!cat).)*)cat', '\\1Bull', s)

#Warning: this code generation is experimental. Please inspect the code for any bugs before executing.