import re text = '1;2;3;4;5;1;2;3;4;5;1;2;3;4;5;1;2;3;4;5;1;2;3;4;5;1;2;3;4;5;1;2;3;' print(re.sub(r'((.*?;){4}.*?);', r'\1NACHTOZAMENIMKOMANDIR?', text))
Standard input is empty
1;2;3;4;5NACHTOZAMENIMKOMANDIR?1;2;3;4;5NACHTOZAMENIMKOMANDIR?1;2;3;4;5NACHTOZAMENIMKOMANDIR?1;2;3;4;5NACHTOZAMENIMKOMANDIR?1;2;3;4;5NACHTOZAMENIMKOMANDIR?1;2;3;4;5NACHTOZAMENIMKOMANDIR?1;2;3;