I’ve seen similar questions to this but unfortunately I couldn’t find the exact case for my problem. What I was looking to do is remove all adjacent occurrences of a set substring but keep the first occurrence.
For example, given
s = "USER USER some words USER USER USER words"
substring = "USER"
The output I want would be
"USER some words USER words"
I’ve tried using sub(), split() but I couldn’t find the answer I wanted. Would appreciate any help, thank you.
>Solution :
Try re:
import re
s = "USER USER some words USER USER USER words"
substring = "USER"
s = re.sub(fr"({re.escape(substring)})(\s+\1)+", substring, s)
print(s)
Prints:
USER some words USER words