regex - replace multiple occurrences of any special character by one in python -
i have string like:
string = "happy.....!!!"
and want output like:
new_string = "happy.!"
i know how replace multiple occurrence of special character. can done follows:
line = re.sub('\.+', '.', line)
but want replace special characters ",./\, etc. 1 way write each special character. want know if there easy way write special characters in 1 line.
you can use \w
match non-word character:
line = re.sub(r'\w+', '.', line)
if want replace same special character use:
line = re.sub(r'(\w)(?=\1)', '', line)
Comments
Post a Comment