Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove in Python "-" from spaces and keep them between words?

I have a string with different words in Python. The string is connected with a "-" between the words and the spaces, as shown in the example below.

asiatische-Gerichte----wuerzige-Gerichte----vegetarische-Sommerrolle--------Smokey-Pulled-Pork-Burger----

I tried using the replace and split method to remove the "-" in the spaces. However, I can only get it to remove them all, but I need the hyphens between the words.

My expected result is shown below:

asiatische-Gerichte wuerzige-Gerichte vegetarische-Sommerrolle Smokey-Pulled-Pork-Burger
like image 881
Anon_test Avatar asked Dec 17 '25 15:12

Anon_test


1 Answers

Use re.sub to replace all occurrences of 2 or more dashes in a row with a space:

s = re.sub(r'-{2,}', ' ', s)
like image 70
Timur Shtatland Avatar answered Dec 20 '25 04:12

Timur Shtatland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!