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

gcc - MinGW's ld cannot perform PE operations on non PE output file -

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

c# - Search and Add Comment with OpenXML for Word -