Assume there's a string
"An example striiiiiing with other words"
I need to replace the 'i'
s with '*'
s like 'str******ng'
. The number of '*'
must be same as 'i'
. This replacement should happen only if there are consecutive 'i'
greater than or equal to 3. If the number of 'i'
is less than 3 then there is a different rule for that. I can hard code it:
import re
text = "An example striiiiing with other words"
out_put = re.sub(re.compile(r'i{3}', re.I), r'*'*3, text)
print(out_put)
# An example str***iing with other words
But number of i could be any number greater than 3. How can we do that using regex?