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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -