You can use re.subn which perform the same operation as sub(), but return a tuple (new_string, number_of_subs_made)
If number of modification is 0
i.e. string is not modified.
>>> re.subn('(xx)+', '', 'abcdab')
('abcdab', 0)
>>> re.subn('(ab)+', '', 'abcdab')
('cd', 2)
>>>