import re pat = r"\b(see|under|in|of|with|this)\b( *.{0,4})(item)" s = "i have many roof item in the repeat item of the item inthe item downunder. with any item" res = re.sub(pat, r"\1\2replaced_item", s) print(res)
Standard input is empty
i have many roof item in the repeat item of the replaced_item inthe item downunder. with any replaced_item