I am trying to use regex to remove white spaces in the sequence of consecutive '?' and/or '!' in a string. One example is that "what is that ?? ? ? ?? ??? ? ! ! ! ? !" should be changed to "what is that ??????????!!!?!". That is, I want to concatenate all '?' and '!' without space in between. My current code doesn't work out well:
import re
s = "what is that ?? ? ? ?? ??? ? ! ! ! ? !"
s = re.sub("\? +\?", "??", s)
s = re.sub("\? +\!", "?!", s)
s = re.sub("\! +\!", "!!", s)
s = re.sub("\! +\?", "!?", s)
which produces 'what is that ??? ???????!! !?!', where some spaces are obviously not deleted. what is going wrong in my code and how to revise it?