I have a list of characters that I want to find in a string and replace its multiple occurances together into just one occurance.
But I am facing 2 problems - when i loop over them, the re.sub function does not replace the multiple occurances and when i have a smiley like :) it replaces ':' with ':)' which I dont want.
Here is the code that I tried.
end_of_line_chars = [".",";","!",":)",":-)","=)",":]",":-(",":(",":[","=(",":P",":-P",":-p",":p","=P"]
for i in end_of_line_chars:
pattern = "[" + i + "]" + "+"
str = re.sub(pattern,i,str)
If I take a single character and try it works as shown below.
str = re.sub("[.]+",".",str)
But looping over a list of characters gives error. How to solve these 2 problems? Thanks for the help.
\`in order to actually match those characters. i would suggest usingstring.replace()method instead, why bother withregexstr.replacewon't replace a variable length run of a specific character with a single replacement.